2013-09-22 15 views
0
//Import scanner 
import java.util.Scanner; 

public class PrintCostCalculator{ 

public static void main(String[] args) 
{ 
    //Define variables 
    Scanner keyboard = new Scanner(System.in); 
    final double text = 5000; //Text dots per page 
    final double image = 10000; //Image dots per page 
    final double cText = 15000; //Compressed text dots per page 
    final double statement = 7000; //Statement dots per page 
    final double color = (5e-5); //Color ink cost per dots 
    final double black = (1e-5); //Black ink cost per dots 
    Double estimatedCost = 0.0; 


    //Pages to print 
    System.out.printf("--- Price Estimator Program ---%nEnter Number of Pages (digits only): "); 
    int pagestoPrint = keyboard.nextInt(); 

    //Print Type 
    System.out.printf("%n---- Select a Print Type ----%nEnter T or t for Text%nEnter I or i for Image%nEnter C or c for Compressed Text%nEnter S or s for statement%n---------------------------%nEnter Print Type: "); 
    String pType = keyboard.next(); //pType is print type variable holder 
    char PrintType = pType.charAt(0); 

    //Print Color 
    System.out.printf("%n--- Select a Print Color ---%nEnter 0 for Grayscale%nEnter 1 for Color%n-----------------------------%nEnter Print Color: "); 
    int printColor = keyboard.nextInt(); 
    System.out.printf("-----------------------------%nIs there a sale (y/n): "); 
    String sType = keyboard.next(); //sType is the sale type variable holder 
    String lower =sType.toLowerCase(); 
    char saleType = sType.charAt(0); 
    System.out.print(lower); 

    //Calculation for color printing 
     if (printColor == 1 && lower == "t") { 
       estimatedCost = pagestoPrint * text/color; 
      } else { 
       if (printColor == 1 && lower == "i") { 
        estimatedCost = pagestoPrint * image/color; 
       } else { 
        if (printColor == 1 && lower == "c") { 
         estimatedCost = pagestoPrint * cText/color; 
        } else { 
         if (printColor == 1 && lower == "s") { 
          estimatedCost = pagestoPrint * statement/color; 
        // } else { 
         // System.out.println("oops"); 
         } 
        } 
       } 
      } 

    //Calculation for black printing 
        if (printColor == 0 && lower == "t") { 
           estimatedCost = pagestoPrint * text/black; 
         } else { 
          if (printColor == 0 && lower == "i") { 
           estimatedCost = pagestoPrint * image/black; 
         } else { 
          if (printColor == 0 && lower == "c") { 
           estimatedCost = pagestoPrint * cText/black; 
          } else { 
           if (printColor == 0 && lower == "s") { 
            estimatedCost = pagestoPrint * statement/black; 
          // } else { 
           // System.out.println("oops2"); 
           } 
          } 
         } 

    //EstimatedCost variable change 

    //Cost Estimate 
    System.out.printf("%n--- Cost Estimate ---%nInk Usage Per Page: "); 
    System.out.print(estimatedCost); 
         } 
} 
} 

我的Double estimatedCost不會更新。我是否還有其他陳述是爲什麼?我一直在尋找,我無法找到這個代碼有什麼問題。我在哪裏可以學習更好地解決我的代碼問題?有小費嗎?如何獲得我的「估計成本」雙重更新和打印? NewToJava

+1

使用等於字符串比較。 –

回答

4

我的if if else語句的原因是什麼?

是您的if語句寫得不好,因爲你正在使用==比較字符串。應使用equals方法比較字符串。

例如該檢查

lower == "i" 

應與

"i".equals(lower); 

注意更換:對文字反向檢查避免了NPE的情況下是空

更新你如果條件如上所述並嘗試運行您的程序。

+0

謝謝,這解決了我的問題。我還沒有閱讀關於equals方法。你能推薦一本Java書嗎?我不喜歡我的書。 – ThisGuy

0

我認爲仍然有一些錯誤,這裏有一個正確的版本:

//Import scanner 
import java.util.Scanner; 

public class Main{ 

public static void main(String[] args) { 

    //Define variables 
    Scanner keyboard = new Scanner(System.in); 
    final double text = 5000; //Text dots per page 
    final double image = 10000; //Image dots per page 
    final double cText = 15000; //Compressed text dots per page 
    final double statement = 7000; //Statement dots per page 
    final double color = (5e-5); //Color ink cost per dots 
    final double black = (1e-5); //Black ink cost per dots 
    Double estimatedCost = 0.0; 


    //Pages to print 
    System.out.printf("--- Price Estimator Program ---%nEnter Number of Pages (digits only): "); 
    int pagestoPrint = keyboard.nextInt(); 

    //Print Type 
    System.out.printf("%n---- Select a Print Type ----%nEnter T or t for Text%nEnter I or i for Image%nEnter C or c for Compressed Text%nEnter S or s for statement%n---------------------------%nEnter Print Type: "); 
    String pType = keyboard.next(); //pType is print type variable holder 
    pType = pType.toLowerCase(); //Added 
    char PrintType = pType.charAt(0); 

    //Print Color 
    System.out.printf("%n--- Select a Print Color ---%nEnter 0 for Grayscale%nEnter 1 for Color%n-----------------------------%nEnter Print Color: "); 
    int printColor = keyboard.nextInt(); 
    System.out.printf("-----------------------------%nIs there a sale (y/n): "); 
    String sType = keyboard.next(); //sType is the sale type variable holder 
    String lower =sType.toLowerCase(); 
    char saleType = sType.charAt(0); 


    //Calculation for color printing 
    if (printColor == 1){ 
     if (PrintType == 't') 
       estimatedCost = pagestoPrint * text/color; 

     else if (PrintType == 'i') 
       estimatedCost = pagestoPrint * image/color; 

     else if (PrintType == 'c') 
       estimatedCost = pagestoPrint * cText/color; 

     else if (PrintType == 's') 
       estimatedCost = pagestoPrint * statement/color; 
    } 

    //Calculation for black printing 
    else if (printColor == 0){ 

     if(PrintType == 't') 
       estimatedCost = pagestoPrint * text/black; 
     else if (PrintType == 'i') 
       estimatedCost = pagestoPrint * image/black; 
     else if (PrintType == 'c') 
       estimatedCost = pagestoPrint * cText/black; 
     else if (PrintType == 's') 
        estimatedCost = pagestoPrint * statement/black; 
    } 

    //EstimatedCost variable change 

    //Cost Estimate 
    System.out.printf("%n--- Cost Estimate ---%nInk Usage Per Page: "); 
    System.out.print(estimatedCost); 
} 
    } 

你不能用==比較兩個字符串,則必須使用.equals()代替。用==比較存儲在參考中的地址。

您犯的一個大錯誤是比較「較低」而不是「PrintType」。較低包含「是否有銷售」的答案,您想比較的是印刷類型。所以我改變了它,並在printedType上添加了toLowerCase()操作。