0

問題は、コードを何百万回も見て、単純な答えが見えないだけかもしれませんが、なぜ著者名と著者の姓の時点で、システムは次のように出力するのですか?

Enter author Name: Enter author Surname: 

そして、それはこのようになるはずです...

Enter author Name: 
Enter author Surname:

システムに情報を入力すると、著者の名だけが記憶され、姓の情報を入力できません。

import java.util.Scanner;

public class BookTest {

    public static void main(String[] args) {    

        Scanner scan = new Scanner (System.in);

        Book bookIn = new Book();

        System.out.print("Enter book title: ");
        bookIn.setTitleBook(scan.nextLine());



        System.out.print("Enter author Name: ");
        bookIn.setFirstName(scan.nextLine());

        System.out.print("Enter author Surname: ");
        bookIn.setSecondName(scan.nextLine());

        System.out.print("Enter author Nationality: ");
        bookIn.setNationality(scan.nextLine());




        System.out.print("Enter book price: ");
        bookIn.setPriceBook(scan.nextDouble());

        System.out.print("Enter book ISBN: ");
        bookIn.setIsbn(scan.nextInt());




        System.out.println("Title: " +bookIn.getTitleBook());
        System.out.println("Author: "+ bookIn.getFirstName()+ " " + bookIn.getSecondName());
        System.out.println("Price: "+bookIn.getPriceBook());
        System.out.println("ISNB Number: "+bookIn.getIsbn());
        System.out.println("Aurhor Nationality: " + bookIn.getNationality());



    }
}

ブッククラス

public class Book {

    private double priceBook;
    private int isbn;
    private String titleBook;
    private String firstName;
    private String secondName;
    private String nationality;


    public void setTitleBook(String titleBook){
        this.titleBook = titleBook;
    }
    public String getTitleBook(){
        return titleBook;
    }


    public void setPriceBook(double priceBook) {
        this.priceBook = priceBook;
    }
    public double getPriceBook() {
        return priceBook;
    }


    public void setIsbn(int isbn) {
        this.isbn = isbn;
    }
    public int getIsbn() {
        return isbn;
    }



    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getFirstName() {
        return firstName;
    }




    public void setSecondName(String secondName) {
        this.secondName = secondName;
    }
    public String getSecondName() {
        return secondName;
    }


    public void setNationality(String nationality) {
        this.nationality = nationality;
    }
    public String getNationality() {
        return nationality;
    }



}
4

1 に答える 1

1

ISBN を読み取った後、スキャナーを次の行に移動します。入力を使用Scanner.nextIntする場合は、Enter キーを押したときに作成される改行文字ではなく、数字のみを読み取ります。Scanner.nextLineafterを呼び出すScanner.nextIntと、改行文字が読み取られ、バッファーがクリアされます。

import java.util.Scanner;

public class BookTest {

    public static void main(String[] args) {    

        Scanner scan = new Scanner (System.in);

        Book title = new Book();
        Book price = new Book();
        Book isbn = new Book();
        Book fName = new Book();
        Book sName = new Book();
        Book nation = new Book();

        System.out.print("Enter book title: ");
        title.setTitleBook(scan.nextLine());

        System.out.print("Enter book price: ");
        price.setPriceBook(scan.nextDouble());

        System.out.print("Enter book ISBN: ");
        isbn.setIsbn(scan.nextInt());

        scan.nextLine(); //NOTICE CHANGE HERE
        //System.out.println(); THIS WAS REMOVED


        System.out.print("Enter author Name: ");
        fName.setFirstName(scan.nextLine());

        System.out.print("Enter author Surname: ");
        sName.setSecondName(scan.nextLine());

        System.out.print("Enter author Nationality: ");
        nation.setNationality(scan.nextLine());


    System.out.println("Title: " +title.getTitleBook());
    System.out.println("Price: "+price.getPriceBook());
    System.out.println("ISNB Number: "+isbn.getIsbn());

    System.out.println("Author: "+ fName.getFirstName() + sName.getSecondName());
    System.out.println("Aurhor Nationality: " + nation.getNationality());



    }
}
于 2013-02-26T10:31:44.773 に答える