java为什么多个nextInt()可以工作?
在下面的Java代码中
Scanner input = new Scanner(System.in); //Cmd1
int i1,i2; //Cmd2
i1 = input.nextInt(); //Cmd3
System.out.println("value of i1 is "+ i1); //Cmd4
i2 = input.nextInt(); //Cmd5
System.out.println("value of i2 is "+ i2); //Cmd6
String s1; //Cmd7
input.nextLine(); //Cmd8
s1 = input.nextLine(); //Cmd9
System.out.println("value of s1 is "+ s1); //Cmd10
我在控制台中获得了以下输出
<myinput> 1
<output> value of i1 is 1
<myinput> 2
<output> value of i2 is 2
<myinput> firstString
<output> value of s1 is firstString
<;myinput>;一直等待输入,直到我按下enter
键。我尝试了tab
,多次tab
,space
,但它一直在等待,没有移动到下一个命令<;产量>;因此,我假设nextInt会等到它在输入流中找到\n后再转到下一个命令
查看来自Scanner is skipping nextLine() after using next() or nextFoo()?的答案,我们必须添加Cmd8
的原因是,nextInt()继续读取输入,直到\n
,将输入发送到程序并将\n
放回输入流的前面。现在,当“Cmd8”中的nextLine()开始读取它时,它在输入流的开始处发现\n
,没有任何前面的字符串,它假定用户没有输入任何内容,因此将空字符串作为输入并转到Cmd9
我的疑问是,因为nextInt()也读取till\n
(根据我的经验,这意味着Cmd3
一直在等待,直到我按下enter
),为什么Cmd5
在等待我的输入。它还应该在输入流的开头找到\n
,并假设我没有输入任何内容,并将空整数作为输入(类似于Cmd8
的行为)。这里发生了什么
# 1 楼答案
我已经通过调试JRE Scanner类方法分析了您的问题。当您调用
nextInt
函数时,扫描器会查看当前缓冲区。如果没有可用的输入,则开始监听键盘,直到按Enter
键,整个键盘字符追加到缓冲区,并尝试匹配它是否为整数。当找到它时,整数nextInt
返回值并递增缓冲区的位置索引。下一步,您再次调用了nextInt
,并且只有\n
字符,然后丢弃它并等待下一个整数模式。此外,缓冲区的下一个字符也是\n
。调用nextLine
时,它会查看缓冲区并打印空行。所以第二个nextLine
是从空文本开始的也许这会让人困惑。让我们看一下场景:
注意:索引
^
表示缓冲区的读取位置返回
12
,下一个缓冲区位置位于\n
返回
34
,下一个缓冲区位置位于第二个\n
缓冲区具有
\n
字符,所以扫描仪不会等待键盘并返回值返回
empty text
返回
test
# 2 楼答案
请注意^{} 文件中的以下摘录:
Scanner
使用的默认分隔符是\p{javaWhitespace}+
,您可以通过以下方式获得:下面的代码行显示了在找到
10
或hello
时如何找到完整的标记,即,无论您按多少次Enter,next()
,nextInt()
等都会一直请求输入,直到输入非空白字符注意:在nextInt()
的情况下,值10
可解析为int
,但hello
不可解析,因此InputMismatchException
将被抛出输出:
这适用于所有{},但{}除外,其{a2}(下面给出的摘录)非常清楚其行为与其他{}不同
换句话说,
nextLine()
将捕获所有内容,包括直接输入的空白字符或先前输入的剩余字符