2017-02-12 6 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"변수가 초기화되지 않았습니다. 사용하기 전에 값을 설정해야합니다. 또한 "부분합"변수를 두 번 만듭니다. 반면에 지역 변수 "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 : 루프는 당신을 위해,의 때문에 수행 오 당신이 bookSubtotal에 값을 설정해야합니다, 루프 또한

(하나의 내가 다음 this line에 쓴)에있는

counter++; 

줄을 제거 :

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 + "."); 
      } 
} 
관련 문제