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