为什么扫描仪会跳过用户的输入

发布于 2024-10-10 09:43:01 字数 1564 浏览 0 评论 0原文

我有一个小任务,允许用户输入任何国家的地区及其邻居。

我做了所有的事情,只是有一个小问题,那就是当我运行我的代码时,程序要求用户输入区域的数量,如果用户输入 13 或数字大于 10,系统会认为该数字就像两个输入,它不会允许用户为第二个问题输入任何内容,并且会立即提示他第三个问题。为什么?

我认为 Scanner 类的问题在以下命令中:

Scanner kb = new Scanner(System.in);
System.out.print("Please enter the number of regions: ");
        int REGION_COUNT = kb.nextInt();
        region = new CountryRegion[REGION_COUNT]; 
        String[] neighbours;
        for (int r = 0; r < region.length; r++) {
            System.out.print("Please enter the name of region #" + (r + 1) + ": ");
            String regionName  = kb.nextLine();
            System.out.print("How many neighbors for region #" + (r + 1) + ": ");
            if (kb.hasNextInt()) {
                int size = kb.nextInt();
                neighbours = new String[size];
                for (int n = 0; n < size; n++) {
                    System.out.print("Please enter the neighbour #" + (n + 1) + ": ");
                    neighbours [n] = kb.nextLine();
                }
                region [r] = new CountryRegion(regionName, neighbours);

            } 
            else
                System.exit(0); 
        }
        for (int i = 0; i < REGION_COUNT; i++) {
            System.out.print(region[i].getRegionName() +": ");
            for (int k = 0; k < region[i].getRegionAjesint().length; k++) {
                System.out.print(region[i].getRegionAjesint()[k] +", ");
            }
            System.out.println();
        }
        mapColor = new MapColor(region);

请问有什么帮助吗?

I have a small task that allows the user to enter the regions and their neighbors of any country.

I did everything and I just have a small problem which is when I run my code and the program asks the user to enter the number of regions, if the user enters 13 or and number greater than 10, the system will consider that number is like two inputs and it will not allow the user to enter anything for the second question and it will prompt him with the third question immediately. Why?

I think the problem with the Scanner class in the following command:

Scanner kb = new Scanner(System.in);
System.out.print("Please enter the number of regions: ");
        int REGION_COUNT = kb.nextInt();
        region = new CountryRegion[REGION_COUNT]; 
        String[] neighbours;
        for (int r = 0; r < region.length; r++) {
            System.out.print("Please enter the name of region #" + (r + 1) + ": ");
            String regionName  = kb.nextLine();
            System.out.print("How many neighbors for region #" + (r + 1) + ": ");
            if (kb.hasNextInt()) {
                int size = kb.nextInt();
                neighbours = new String[size];
                for (int n = 0; n < size; n++) {
                    System.out.print("Please enter the neighbour #" + (n + 1) + ": ");
                    neighbours [n] = kb.nextLine();
                }
                region [r] = new CountryRegion(regionName, neighbours);

            } 
            else
                System.exit(0); 
        }
        for (int i = 0; i < REGION_COUNT; i++) {
            System.out.print(region[i].getRegionName() +": ");
            for (int k = 0; k < region[i].getRegionAjesint().length; k++) {
                System.out.print(region[i].getRegionAjesint()[k] +", ");
            }
            System.out.println();
        }
        mapColor = new MapColor(region);

Any help, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩动你心 2024-10-17 09:43:01

好的,非常简单,您的问题是您正在使用 Scanner 类的 nextInt() 方法,然后使用 nextLine() 方法,这两个方法都使用相同的缓冲区并且这就是正在发生的事情。

当您在键盘中输入您要求的数字(假设 10)时,您实际输入的数字

10 和回车键(换行符 (\n))

Scanner 类中的 nextInt() 方法将读取 10,并且仅读取 10,这意味着新的行字符行字符 (\n) 仍在键盘缓冲区中,接下来在代码中您有一个 nextLine() 它将读取新行之前的所有内容 (\n),你已经在缓冲区中了!

因此,这一切的工作方式是 nextLine() 方法在输入时考虑留在缓冲区中的新行字符 (\n),并继续到循环的下一次迭代。

问题的解决方案是清除新行字符 (\n) 的缓冲区,您可以通过在实际的行字符之前调用 nextLine() 方法来实现此目的像这样的代码:

...
int REGION_COUNT = kb.nextInt();
region = new CountryRegion[REGION_COUNT]; 
String[] neighbours;
kb.nextLine(); //CLEAR THE KEYBOARD BUFFER
for (int r = 0; r < region.length; r++) {
     System.out.print("Please enter the name of region #" + (r + 1) + ": ");
     String regionName  = kb.nextLine();
...

这样,调用的 nextLine() 从缓冲区中提取新行字符,清除它,并且由于它不存储它,所以它被丢弃,为您留下一个新行字符空闲缓冲区准备好在 nextLine() 方法中接收来自用户的完整输入。

希望这有帮助。

Ok, Very simple your problem is that you are using the nextInt() method of the Scanner class and then using the nextLine() method both of these use the same buffer and here is what's happening.

When you enter the number your asking (let say 10) in the key board your actually entering

10 and the enter key (new line character (\n))

The nextInt() method from the Scanner class will read the 10 and just the 10 that meaning that the new line character (\n) is still in the keyboard buffer and next in your code you have a nextLine() which will read everything up to a new line (\n), which you already have in the buffer!!!

So the way this is all working is that the nextLine() method considers the new line character (\n) left in the buffer as it's input and there for continues to the next iteration of the loop.

The solution to your problem is to clear the buffer of the new line character (\n) you can achieve this by calling a nextLine() method before the actual one in your code like so:

...
int REGION_COUNT = kb.nextInt();
region = new CountryRegion[REGION_COUNT]; 
String[] neighbours;
kb.nextLine(); //CLEAR THE KEYBOARD BUFFER
for (int r = 0; r < region.length; r++) {
     System.out.print("Please enter the name of region #" + (r + 1) + ": ");
     String regionName  = kb.nextLine();
...

This way the nextLine() called extracts the new line character from the buffer, clearing it, and since it doesn't store it it gets discarded leaving you with a new line character free buffer ready to receive full input from the user in your nextLine() method.

Hope this helps.

空‖城人不在 2024-10-17 09:43:01

听起来每一次按键都是一次输入。

Sounds like each key press is an input.

你的呼吸 2024-10-17 09:43:01

您正在使用 nextLine() 来获取字符串,但我认为您应该使用 next()。

You are using nextLine() to get the String but I think you should be using next().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文