如何使用户不必多次输入

时间:2017-11-11 20:45:13

标签: java while-loop java.util.scanner

    public int displayMenuGetChoice(){


    System.out.println("Specialty Auto");
    System.out.print("\n(1) Display the Entire automobile list"
                       + "\n(2) Display the entire automobile inventory sorted by year"
                       + "\n(3) Display automobiles available for sale"
                       + "\n(4) Display list of recently sold automobiles"
                       + "\n(5) Output a mailing list of recent buyers"
                       + "\n(6) Find automobile by VIN number"
                       + "\n(7) Quit"
                       + "\n\nEnter your choice (1-7): ");
        while (!keyboard.hasNextInt() || keyboard.nextInt() < 1 || keyboard.nextInt() > 7){
            System.out.println("Invalid Input");
            System.out.println("Specialty Auto");
            System.out.print("\n(1) Display the Entire automobile list"
                               + "\n(2) Display the entire automobile inventory sorted by year"
                               + "\n(3) Display automobiles available for sale"
                               + "\n(4) Display list of recently sold automobiles"
                               + "\n(5) Output a mailing list of recent buyers"
                               + "\n(6) Find automobile by VIN number"
                               + "\n(7) Quit"
                               + "\n\nEnter your choice (1-7): ");
        keyboard.nextInt();
        }       

        return keyboard.nextInt();

}

这是我所拥有的,我需要验证以确保用户输入数字1到7,但也没有输入任何字母,它的工作类型。当我测试它时,我没有做出故意的错误(使用数字1)我必须在正确的对话框出现之前输入3次。我做错了什么?

编辑:我不确定你为什么标记到其他帖子,它没有为我清除任何东西,这似乎是一个类似但不同的问题。

0 个答案:

没有答案