无法让 Java do/while 正确处理

Can't get Java do/while to process correctly

我已经查看了其他几个与此类似的问题,但我阅读的问题并没有帮助解决我的问题。我的目标是根据 int 用户输入打印菜单值,并继续提供选择,直到用户按 6 退出。这是我的代码(我是 Java 的新手)。

class BookCategories {

    public static void main(String args[]) throws java.io.IOException {

        String menu = "MENU: \n1. Science Fiction \n2. Computers and Technology \n3. Cooking \n4. Business \n5. Comics \n6. Exit";

        Map<Integer, String> bookMap = new HashMap<Integer, String>();
        bookMap.put(1, "You selected Science Fiction. This is a fun topic.");
        bookMap.put(2, "You selected Computers and Technology. This is a complex topic.");
        bookMap.put(3, "You selected Cooking. This is a passionate topic.");
        bookMap.put(4, "You selected Business. This is a serious topic.");
        bookMap.put(5, "You selected Comics. This is an entertaining topic.");
        // bookMap.put(6, " ");

        int x;

        do {
            System.out.println(menu);
            System.out.println("Select an item from the Menu, then press Enter: ");

            x = (int) System.in.read();

            if (x == 6)
                System.out.println("You exited the menu.");
            else {
                System.out.println(bookMap.get(x));
                if (x < 1 || x > 6)
                    System.out.println("Invalid entry. Please enter a number from 1 to 6.");
                else
                    System.out.print(menu);
                System.out.println("Select an item from the Menu, then press Enter.");
            }

        } while (x != 6);
    }

}

正如@user15793316 已经说过的,System.in.read() 没有读取实际数字。您可以使用 Scanner 代替:

Scanner scanner = new Scanner(System.in);

do {
  // ...
  x = scanner.nextInt();
  scanner.nextLine(); // wait for Enter
  // ...
}

嗯,出于某种原因 System.in.read();工作不正常,所以我这样更改您的代码:

public static void main(String[] args) throws java.io.IOException{

    String menu = "MENU: \n1. Science Fiction \n2. Computers and Technology \n3. Cooking \n4. Business \n5. Comics \n6. Exit";
    Scanner scanner = new Scanner(System.in); // this line

    Map<Integer, String> bookMap = new HashMap<Integer, String>();
    bookMap.put(1, "You selected Science Fiction. This is a fun topic.");
    bookMap.put(2, "You selected Computers and Technology. This is a complex topic.");
    bookMap.put(3, "You selected Cooking. This is a passionate topic.");
    bookMap.put(4, "You selected Business. This is a serious topic.");
    bookMap.put(5, "You selected Comics. This is an entertaining topic.");
    // bookMap.put(6, " ");

    int x;

    do {
        System.out.println(menu);
        System.out.println("Select an item from the Menu, then press Enter: ");

        x = scanner.nextInt(); // and this line

        if (x == 6)
            System.out.println("You exited the menu.");
        else {
            System.out.println(bookMap.get(x));
            if (x < 1 || x > 6)
                System.out.println("Invalid entry. Please enter a number from 1 to 6.");
            else
                System.out.print(menu);
            System.out.println("Select an item from the Menu, then press Enter.");
        }

    } while (x != 6);

}