为什么Scanner会跳过用户的输入 [英] Why is Scanner skipping inputs from the user

查看:214
本文介绍了为什么Scanner会跳过用户的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小任务,允许用户进入任何国家的地区和他们的邻居。

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

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

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?

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

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

请帮忙吗?

推荐答案

好的,非常简单你的问题是你正在使用Scanner类的 nextInt()方法,然后使用 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.

当您在键盘上输入您实际输入的数字(比如说10)

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


10和回车键(新行字符( \ n ))

Scanner类中的 nextInt()方法将读取10和10,表示新行字符( \ n )仍然在键盘缓冲区中,然后在你的代码中你有一个 nextLine(),这将读到一切新的你已经在缓冲区中的行( \ 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!!!

所以这一切的工作方式是 nextLine()方法考虑换行符( \ n )在缓冲区中输入,并继续循环的下一次迭代。

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.

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

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

这样 nextLine()调用从缓冲区中提取新行字符,清除它,并且由于它不存储它,它会被丢弃,留下一个新的行字符空闲缓冲区,准备好接收来自用户的完整输入 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.

希望这有帮助。

这篇关于为什么Scanner会跳过用户的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆