/**私は、注文したい書籍の数を入力するように求めています。そして、各書籍のコストを見つけ出し、それらを合計して領収書を与えます彼らの命令のために最後に。 。私はあなたが二度カウンタをインクリメントするように見える*/ユーザー入力によるループの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"変数は初期化されていません。使用する前にその値を設定する必要があります。さらに、 "小計"変数を2回作成します。一方、ローカル変数 "priceOfBooks"はforループで定義されているため、外部ではなく内部で使用することができます。 –
''私のループに問題があります " - 何が問題なの?私たちはあなたの画面をここから見ることができず、問題を説明します。 – David