Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 扫描仪问题,输入信息不工作_Java_Input_Output - Fatal编程技术网

Java 扫描仪问题,输入信息不工作

Java 扫描仪问题,输入信息不工作,java,input,output,Java,Input,Output,问题是,我无数次地查看代码,可能我只是看不到简单的答案,但为什么在作者姓名和作者姓氏方面,系统会这样打印出来: Enter author Name: Enter author Surname: 应该是这样的 Enter author Name: Enter author Surname: 若我在系统中输入信息,只记得作者的名字,我不能输入姓氏信息 import java.util.Scanner; public class BookTest { public static v

问题是,我无数次地查看代码,可能我只是看不到简单的答案,但为什么在作者姓名和作者姓氏方面,系统会这样打印出来:

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;
    }



}

读取ISBN后,将扫描仪移至下一行。使用Scanner.nextInt时,输入仅读取数字,而不读取按enter键时创建的新行字符。在
Scanner.nextInt
之后调用
Scanner.nextLine
读取新行字符并清除缓冲区

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());



    }
}

是这样的吗?Scanner scaner=新扫描仪(System.in);System.out.print(“输入作者姓名:”);fName.setFirstName(scaner.nextLine());System.out.print(“输入作者姓名:”);sName.setSecondName(scaner.nextLine());系统输出打印(“输入作者国籍:”);nation.setNational(scaner.nextLine());它应该会起作用。我不确定你在这篇评论中到底问了什么。请在我发布的解决方案中查找“注意此处的更改”的注释。如果可能,请解释原因。。。。它不能与一台扫描仪一起工作(实际上它只在一个阶段出现bug,我不太明白为什么)?我想你可能希望在同一个图书对象中包含这些信息。不要创建六个不同的Book对象,只创建一个并调用该对象上的setter方法。对不起,你能解释一下吗?当然。您有一个包含6个字段的Book对象。现在在测试代码中,您将创建六个不同的对象,并在每个对象中设置一个字段。但是你可能需要做的是在同一个对象中设置所有六个字段。是这样的吗?我编辑我的代码。。。