/** 주문할 책의 수에 대한 사용자 입력을 요청한 다음 각 책의 비용을 찾아서 합산하여 영수증을 제공하려고합니다. 그들의 명령에 대한 끝에. . 나는 당신이 두 번 카운터를 증가하는 것 */사용자 입력이있는 루프를위한 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"변수가 초기화되지 않았습니다. 사용하기 전에 값을 설정해야합니다. 또한 "부분합"변수를 두 번 만듭니다. 반면에 지역 변수 "priceOfBooks"는 for 루프에 정의되어 있으므로 바깥 쪽이 아닌 내부에서만 사용할 수 있습니다. –
''내 루프에 문제가있다 ''- 무슨 "문제"? 여기에서 화면을 볼 수없고 문제를 설명 할 수 없습니다. – David