2017-02-12 7 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"変数は初期化されていません。使用する前にその値を設定する必要があります。さらに、 "小計"変数を2回作成します。一方、ローカル変数 "priceOfBooks"はforループで定義されているため、外部ではなく内部で使用することができます。 –

+0

''私のループに問題があります " - 何が問題なの?私たちはあなたの画面をここから見ることができず、問題を説明します。 – David

答えて

0

を彼らにちょうど私のループに問題を有する出力を与える方法を理解する:あなたはcounterをインクリメントすることをthis lineに何が起こる

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 

} 

があるが、その行でcounter++があなたのためにcounterをインクリメント

for(counter = 0;counter<numberOfBooks;counter++) 

、S:ループは、そのあなたのために、のために行いますOだけforループ(1私はへthis line次の書いた)

counter++; 

行を削除する。また、あなたはbookSubtotalに値を設定する必要があります。

int bookSubtotal = 0; 

初めに。

また、あなたがnumberOfBooks整数にしたいかもしれません:

int numberOfBooks = in.nextInt(); 

をそして、あなたは再びちょうどワード線でdouble削除し、二回subtotalを宣言するべきではありません。

double subtotal = (double)numberOfBooks * priceOfBooks; 

をも行います後にtaxPaidがあるので、ループの前に作成taxpaidが必要です。名前は大文字と小文字が区別され、大文字はImpOrtaNtです。

+0

Itamar Greenは非常に役に立ち、ありがとうございました。 – 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 + "."); 
      } 
} 
関連する問題