意外输出使用 while 循环和 hasNext() 在 Java 中。

huangapple go评论76阅读模式
英文:

Unexpected output using ,while loop and hasNext() in java

问题

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i=1;
        while(scan.hasNext()){
            System.out.println(i + " " + scan.nextLine());
            i++;
        } 
    }
}

OUTPUT:

a
1 a
b
2 b
c
3 c

e
4 
5 e

PROBLEM: 当我输入'a'时,输出与预期相符;对于'b'和'c'也是一样的。
但是当我按下回车键而不是输入'd'时,预期输出是:"4 ",但实际上没有输出任何内容;当我在按下回车键后输入'e'时,会打印出"4 "和"5 e"。有人可以指出我在这里忽略了什么吗?

英文:
import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i=1;
        while(scan.hasNext()){
            System.out.println(i + " " + scan.nextLine());
            i++;
        } 
    }
}

OUTPUT:

a
1 a
b
2 b
c
3 c

e
4 
5 e

PROBLEM : when i have entered , a the output is what it is expected , for b and c is also the same.
but i press enter instead of d the expected out put is :"4 " but it doesnt output 4 , instead no output is given and when i input e after press enter both 4"space" and 5 e are printed. Can someone please point what i am missing to see here?

答案1

得分: 3

因为您使用了nextLine方法,所以出现了这种情况。当您按下<kbd>Enter</kbd>而没有输入字符时,hasNext()会等待下一个字符。当您输入'e'然后按下<kbd>Enter</kbd>时,hasNext()会返回。但现在您已经输入了两行文本:一行为空,另一行包含"e"。对nextLine的调用会返回空行,而在循环的下一次运行中,它会返回包含"e"的行。

要阻止这种情况发生,不要混合使用hasNext和nextLine,可以使用hasNext/next或hasNextLine/nextLine。两者的区别在于前者按空格分隔计数"单词",而后者按行计数。

while(scan.hasNextLine()){
    System.out.println(i + " " + scan.nextLine());
    i++;
}
英文:

This happens because you are using the nextLine method. When you press <kbd>Enter</kbd> without entering a character, hasNext() waits until the next character. When you enter e and then press <kbd>Enter</kbd>, hasNext() returns. But you have now entered two lines of text: one empty, and one with "e". The call to nextLine returns the empty line, and on the next run of the loop it returns the line with "e".

To stop this from happening, don't use a mixture of hasNext and nextLine, use either hasNext/next or hasNextLine/nextLine. The difference is that the first counts "words" separated by whitespace, the second counts lines.

    while(scan.hasNextLine()){
        System.out.println(i + &quot; &quot; + scan.nextLine());
        i++;
    } 

答案2

得分: 2

nextLine()方法会跳过当前行并返回被跳过的输入内容,这就是为什么每次你按下回车键却没有输入时它会计数。如果你想避免跳过输入,可以尝试使用scan.next()

英文:

nextLine() of scanner past the current line and return the input that was skipped, it's why it counts every time you enter down without inputs, if you want to avoid the skip, you can try scan.next()

huangapple
  • 本文由 发表于 2020年8月21日 19:11:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63521784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定