/**我想要求用户输入他们要订购的书籍数量,然后使用它来查找每本书的成本,总计它们并将它们的收据在他们的订单结束。我知道如何让他们只是玩弄我的回路故障输出*/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 + ".");
}
}
“bookSubtotal”变量未初始化。你应该在使用之前设置它的值。此外,您创建两次“小计”变量。另一方面,在for循环中定义了局部变量“priceOfBooks”,以便您可以在其内部而不是外部使用它。 –
'“我的循环有问题'' - 什么”麻烦“?我们无法从这里看到你的屏幕,描述问题。 – David