2016-11-13 55 views
0

我正在爲我的AP Comp Sci類做這項工作,我需要編寫一個程序(使用Eclipse),詢問用戶購買的物品數量,並且需要使用do-while循環創建代碼。每一次通過循環必須涉及不同的項目。對於這些項目中的每一項,用戶都需要輸入成本。然後,我需要打印所有項目的總成本和9%銷售稅後的總額並打印結果。編碼do-while循環?

我試過這樣做,但無法正確打印總數,如果我取出for循環,它不起作用。你能給我一些關於如何解決它的指針嗎?

public static void main(String[] args) { 
    Scanner kbReader = new Scanner(System.in); 

    System.out.println("You went shopping on Cyber Monday."); 

    do { 
     System.out.println("How many items did you purchase?"); 
     int num = kbReader.nextInt(); 
     for(int x = 0; x < num;) { 
      System.out.println("What did you purchase?"); 
      String item = kbReader.next(); 
      System.out.println("How much did it cost?"); 
      double cost = kbReader.nextDouble(); 
      break; 
     } 
     break; 
    } while(true); 

    double total = cost; 
    double totalTax = total + (total * 0.09); 
    System.out.println("Total: $" + total); 
    System.out.println("Total after tax: $" + totalTax); 
} 
+1

歡迎來到Stack Overflow!它看起來像你需要學習使用調試器。請幫助你一些[補充調試技術](https:// ericlippert。COM/2014/03/05 /如何調試的小程序/)。如果您之後仍然有問題,請隨時返回更多詳情。 –

回答

0

你實現

有三個錯誤在代碼:

  1. x++在for循環失蹤

  2. 你應該申報cost ouside的do-while環你應該總結(cost += kbReader.nextDouble();)單個項目的費用在for循環中。

  3. 您不應該打破for循環(當x達到目標數字時它會自動中斷)。

而且不要忘了do-while後關閉掃描儀:kbReader.close();

此代碼應工作:

public static void main(String[] args) { 
     Scanner kbReader = new Scanner(System.in); 

     System.out.println("You went shopping on Cyber Monday."); 
     double cost = 0; 
     do 
     { 
     System.out.println("How many items did you purchase?"); 
     int num = kbReader.nextInt(); 
     for(int x = 0; x < num;x++) 
     { 
      System.out.println("What did you purchase?"); 
      String item = kbReader.next(); 
      System.out.println("How much did it cost?"); 
      cost += kbReader.nextDouble(); 
     } 
     break; 
     }while(true); 
     kbReader.close(); 
     double total = cost; 
     double totalTax = total + (total * 0.09); 
     System.out.println("Total: $" + total); 
     System.out.println("Total after tax: $" + totalTax); 
    } 

正確的DO-而實現

我會喜歡建議你另一個實現,因爲我認爲如果你使用do-while您也不應該使用for循環。 這是代碼:

Scanner kbReader = new Scanner(System.in); 
System.out.println("You went shopping on Cyber Monday."); 
double cost = 0; 
int x = 0; 
System.out.println("How many items did you purchase?"); 
int num = kbReader.nextInt(); 
do 
{ 
    System.out.println("What did you purchase?"); 
    String item = kbReader.next(); 
    System.out.println("How much did it cost?"); 
    cost += kbReader.nextDouble(); 
    x++; 
} while(x<num); 
kbReader.close(); 
double total = cost; 
double totalTax = total + (total * 0.09); 
System.out.println("Total: $" + total); 
System.out.println("Total after tax: $" + totalTax); 
0

您的代碼有幾個問題。第一個問題是,它們只能運行一次後會突破兩個循環。

while (true)通常不是一個好主意。我建議你問他有多少項目,而循環的上面做的,並通過一個while循環體內設置一個計數器爲0遞增計數器,然後在while語句測試它是這樣:

int count = 0; 
do { 
    <code body> 
    count++; 
} while (count < num); 

此外,你試圖訪問一個總是在while循環範圍內的變量。它不會讓你編譯。在while循環之上初始化它,並在循環內部增加它(例如total += cost;)。你可以完全擺脫for循環。

試試看!