2017-02-12 84 views
-2

/**我想要求用戶輸入他們要訂購的書籍數量,然後使用它來查找每本書的成本,總計它們並將它們的收據在他們的訂單結束。我知道如何讓他們只是玩弄我的迴路故障輸出*/Java寫作循環用戶輸入

import java.util.Scanner; 

public class BookOrder { 
    public static void main(String[] orgs){ 
     Scanner in = new Scanner(System.in); 
     final double TAX = .065; 
     final double SHIPPING = 2.95; 
     int counter = 0; 

     double bookSubtotal, subtotal, taxPaid; 
     System.out.print("Please enter the number of books you're ordering: "); 
     double numberOfBooks = in.nextDouble(); 
     for (counter = 0; counter < numberOfBooks; counter++){ 
      System.out.println("Please enter the cost of your book: "); 
      double priceOfBooks = in.nextDouble(); 
      bookSubtotal = priceOfBooks + bookSubtotal; 
      counter ++; 

     } 

     double subtotal = numberOfBooks * priceOfBooks; 
     double taxpaid = subtotal * (TAX); 
     double shippingCharge = SHIPPING * numberOfBooks; 
     double sumOfOrder = bookSubtotal + priceOfOrder + shippingCharge + TAX; 

      System.out.println("Number of books purchased:" + numberOfBooks); 
      System.out.println("Book subtotal: $" + subtotal); 
      System.out.println("Tax: $" + taxPaid); 
      System.out.println("Shipping: $" + shippingCharge); 
      System.out.println("-------------------------------"); 
      System.out.println("The price of the order is $" + sumOfOrder + "."); 
    } 
} 
+0

「bookSubtotal」變量未初始化。你應該在使用之前設置它的值。此外,您創建兩次「小計」變量。另一方面,在for循環中定義了局部變量「priceOfBooks」,以便您可以在其內部而不是外部使用它。 –

+0

'「我的循環有問題'' - 什麼」麻煩「?我們無法從這裏看到你的屏幕,描述問題。 – David

回答

0

你似乎計數器加一兩次。

for (counter = 0; counter < numberOfBooks; counter++){ 
     System.out.println("Please enter the cost of your book: "); 
     double priceOfBooks = in.nextDouble(); 
     bookSubtotal = priceOfBooks + bookSubtotal; 
     counter ++;//this line 

} 

this line會發生什麼事是你增加counter,但循環會替你,是因爲:

for(counter = 0;counter<numberOfBooks;counter++) 

在該行的counter++遞增counter你,S Ø只是刪除在for循環(一個我寫this line旁)

另外,

counter++; 

線,你應該值設置爲bookSubtotal

int bookSubtotal = 0; 

在開始。

此外,你可能想使numberOfBooks整數:

int numberOfBooks = in.nextInt(); 

,你不應該重新申報subtotal兩次,只是刪除字double在該行:

double subtotal = (double)numberOfBooks * priceOfBooks; 

也不行您需要在循環之前創建taxpaid,因爲您之後有taxPaid。命名區分大小寫,表示大寫字母是ImpOrtaNt ...

+0

非常感謝你Itamar綠色,這是非常有益的,並讓我在我需要去的地方! – Matt

+0

@Matt那麼你應該接受答案。 – ItamarG3

0
public class BookOrder { 
    public static void main(String[] orgs){ 
     Scanner in = new Scanner(System.in); 
     final double TAX = .065; 
     final double SHIPPING = 2.95; 
     int counter = 0; 

     double bookSubtotal = 0; 
     System.out.print("Please enter the number of books you're ordering: "); 
     int numberOfBooks = in.nextInt(); 
     for (counter = 0; counter < numberOfBooks; counter++){ 
      System.out.println("Please enter the cost of your book: "); 
      double priceOfBooks = in.nextDouble(); 
      bookSubtotal += priceOfBooks; 

     } 

     double shippingCharge = SHIPPING * numberOfBooks; 
     double tax = TAX * bookSubtotal; 
     double sumOfOrder = bookSubtotal + shippingCharge + tax; 
      System.out.println("Number of books purchased:" + numberOfBooks); 
      System.out.println("Book subtotal: $" + bookSubtotal); 
      System.out.println("Tax: $" + tax); 
      System.out.println("Shipping: $" + shippingCharge); 
      System.out.println("-------------------------------"); 
      System.out.println("The price of the order is $" + sumOfOrder + "."); 
      } 
}