2011-09-07 9 views
1

"Java, 프로그래밍 방법"이라는 책에서 연습하고 있습니다. 나는 2 개의 클래스로 구성된 작은 프로그램을 만들었습니다. 이 프로그램은 판매 된 품목에 대한 인보이스를 나타 내기 위해 하드웨어 상점에서 사용하기로되어 있습니다. 품목 번호에 대한 문자열 값, 제품을 설명하는 문자열 값, 판매 된 품목 수량에 대한 int 값 및 품목 가격에 대한 이중 값의 4 가지 정보를 포함해야합니다. 나는 main 메소드를 포함하고있는 클래스에서 클래스의 2 개의 객체를 생성했다. 각 인스턴스 변수에 대해 "set 및 get-methods"를 사용해야합니다.System.out.print-method가 올바르게 작동하지 않습니다

문제는 프로그램이 사용자에게 변수 값을 쓰도록 요청할 때 "두 번째 항목 번호"변수 (명령 창의 복사본에있는 5 행)의 첫 번째 값을 읽지 않습니다. . 나는 정말로 이것이 일어나는 이유를 코드에서 읽을 수 없다. 아무도 나를 도울 수 있습니까?

public class aInvoice 
    { 
    private String number; 
    private String description; 
    private int quantity; 
    private double price; 

    public aInvoice(String pNumber, String pDescription, int pQuantity, double pPrice) 
     { 
     number = pNumber; 
     description = pDescription; 
     if (pQuantity < 0) 
     {quantity = 0;} 
     else 
     {quantity = pQuantity;} 
     if (pPrice < 0) 
     {price = 0;} 
     else 
     {price = pPrice;} 
     } 

    public String getNumber() 
     { 
     return number; 
     } 
    public String getDescription() 
     { 
     return description; 
     } 
    public int getQuantity() 
     { 
     return quantity; 
     } 
    public double getPrice() 
     { 
     return price; 
     } 

    double totalAmount; 
    public double getaInvoiceTotalAmount() 
     { 
     return quantity * price; 
     } 
    } 

를 실행 한 다음, 다음과 같이

두 클래스의 코드는

import java.util.Scanner; 
    public class aInvoiceTest 
    { 
    public static void main(String[]args) 
     { 
     String partNumber1 = null; 
     String partDescription1 = null; 
     int partQuantity1 = 0; 
     double partPrice1 = 0.0; 
     String partNumber2 = null; 
     String partDescription2 = null; 
     int partQuantity2 = 0; 
     double partPrice2 = 0.0; 

     Scanner input = new Scanner (System.in); 
     System.out.print("Enter first items number: "); 
     partNumber1 = input.nextLine(); 
     System.out.print("Enter description: "); 
     partDescription1 = input.nextLine(); 
     System.out.print("Enter quantity: "); 
     partQuantity1 = input.nextInt(); 
     System.out.print("Enter price: $"); 
     partPrice1 = input.nextDouble(); 
     System.out.print("Enter second items number: "); 
     partNumber2 = input.nextLine();  
     System.out.print("Enter description: "); 
     partDescription2 = input.nextLine(); 
     System.out.print("Enter quantity: "); 
     partQuantity2 = input.nextInt(); 
     System.out.print("Enter price: $"); 
     partPrice2 = input.nextDouble(); 

     aInvoice aInvoice1 = new aInvoice(partNumber1, partDescription1, partQuantity1, partPrice1); 
     aInvoice aInvoice2 = new aInvoice(partNumber2, partDescription2, partQuantity2, partPrice2); 

     System.out.printf("\n\nPart 1´s item number: %s\nItem description: %s\nQuantity: %d\nPrice each: $ %.2f\n\n", aInvoice1.getNumber(), aInvoice1.getDescription(), aInvoice1.getQuantity(), aInvoice1.getPrice()); 

     System.out.printf("\n\nPart 2´s item number: %s\nItem description: %s\nQuantity: %d\nPrice each: $ %.2f\n\n", aInvoice2.getNumber(), aInvoice2.getDescription(), aInvoice2.getQuantity(), aInvoice2.getPrice()); 

     System.out.printf("Total amount: $ %.2f\n\n", (aInvoice1.getaInvoiceTotalAmount() + aInvoice2.getaInvoiceTotalAmount())); 
     } 
    } 

명령 창에서 읽기는 다음과 같습니다

첫 번째 항목 번호를 입력 : 44 를 입력 설명 : PC 수량 입력 : 1 가격 : $ 10 두 번째 항목을 입력하십시오. num BER : 입력 설명 : 전화 수량을 입력 : $ (100)

부 1's 항목 번호 : (44) 상품 설명 : PC 수량 : 1 개 가격 각 : $ 10.00

부 2'- 1 가 가격을 입력 S 상품 번호 : 상품 설명 : 전화 수량 : 1 개 가격은 각각 : $ 100.00

총 금액 : $ 110.00

답변

2

이 BECA입니다 input.nextInt() 또는 input.nextDouble()을 사용하여 입력을 읽은 후에 사용하려면 다른 문자열을 읽으려고하기 전에 버퍼를 지워야합니다.

사용자가 5.0을 입력하면 5.0이 입력 버퍼에서 가져 오지만 캐리지 리턴이 남아 있습니다. input.nextLine()을 넣으면 캐리지 리턴이 해제됩니다.

그래서 당신의 코드는 기본적으로 문제가 당신이 nextInt/nextDouble를 호출 할 때, 이러한 기능은 사용자의 입력 스트림에 실제로 존재하는 어떤 숫자 부분을 잡아이다

System.out.print("Enter price: $"); 
    partPrice1 = input.nextDouble(); 

    input.nextLine(); 

    System.out.print("Enter second items number: "); 
    partNumber2 = input.nextLine();  
    System.out.print("Enter description: "); 
    partDescription2 = input.nextLine(); 
    System.out.print("Enter quantity: "); 
    partQuantity2 = input.nextInt(); 
    System.out.print("Enter price: $"); 
    partPrice2 = input.nextDouble(); 
+0

감사합니다 : 그냥 등

이것은 당신이보고있는 문제가 나타납니다

, 모든 input.nextInt()input.nextLine();을 넣어! 그게 해결 됐어! – user820913

+0

당신을 위해 일해 주어서 기뻐요. 질문에 대한 답을 표시하여 사람들이 답을 얻을 수 있도록하십시오 :). 나는 당신이 당신의 질문에 답을 표시하지 않았 음을 압니다. 고맙습니다. – samack

관련 문제