当前位置: 首页 > 知识库问答 >
问题:

无法让Java do/while正确处理

夹谷浩博
2023-03-14

我已经回顾了其他几个类似的问题,但我读到的那些问题并没有帮助解决我的问题。我的目标是基于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);
    }

}

共有2个答案

司允晨
2023-03-14

嗯,出于某种原因。在里面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);

}
印瑾瑜
2023-03-14

正如@user15793316已经说过的,System。在里面read()不读取实际数字。您可以改用扫描仪:

Scanner scanner = new Scanner(System.in);

do {
  // ...
  x = scanner.nextInt();
  scanner.nextLine(); // wait for Enter
  // ...
}
 类似资料:
  • 我编写了一个类,其中有一个函数通过接受多个输入,但是运行结果证明它只接受第二个参数,就像接受第一个参数一样,下面是一个简化的代码: 并且输出,丢失: 到底是什么问题?我该怎么解决这个问题?

  • 我只是想知道我是不是做错了什么,因为我对这一切都是陌生的<如果您还有什么需要我补充的,请告诉我 这是回购分支,如果您想查看所有代码,我将尝试在其中实现ViewPager。 所以我有4个用表示的类别,每个类别都包含一个的项目,每个项目都有一个,应该会再现一些音频。 我正在尝试使用ViewPager显示,但问题是当我从滚动到另一个,然后回到已经创建的时,它不会注册触摸事件,什么都不会发生,甚至没有错误

  • 问题内容: 所以我在这里写了一些代码。它本来应该是一个猜谜游戏,但是无论我输入什么内容,它始终会在输出中显示“请输入数字…”,这与我输入的内容无关。基本上,如果“猜测”大于5,则他们猜到了数字。如果不是,那么他们还没有猜到这个数字。这就是游戏的前提。有人可以帮助我修复我的代码,因此无论如何它都不会输出相同的内容? 问题答案: 给你字符。因此,当您输入“ 1”时,它会给您其char值49。因此您不能

  • 我正在做一个新的Django项目,很难得到索引。html页面以正确显示。关于如何更改以使其正确显示,有什么建议吗? 我发现错误了 我对每个文件的设置如下。 我的项目名/我的项目名称/设置.py myprojectname/myprojectname/urls.py django.shortcuts导入render_to_response 我也是Django的新手,所以不要讨厌!:)我很乐意承认我是

  • 问题内容: 例如,\ b退格键打印为四边形(在下面的示例中显示为[])。但是\ n换行符还可以。 我在Vista(Pro),Python 2.7下运行 香港专业教育学院试图谷歌搜索这个问题,并因此找不到任何相关的东西,这似乎很奇怪,我不知道是否有一些设置或其他可能在我的设置中是错误的。不知道要寻找什么。 问题答案: 我做错了什么或应该寻找什么? 期望退格(特别是工作)是否合理? 不,IDLE不支持

  • 将 REST POST 请求从 Angular 前端发送到 Java 后端后,后端接收请求并计算答案。但是,在前端,请求不会显示在日志中,而是给出解析错误。 有一个来自Angularendpoint的静息调用,即: 它在Javaendpoint中接收,该endpoint是: 在后端,日志显示请求到达并且字符串已正确计算。字符串是字母数字组合,没有特殊字符。在前端,chrome 日志不显示请求的痕迹