2017-02-12 148 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 + "."); 
      } 
}