在最后一行读取键盘输入时,扫描仪的奇怪组合

时间:2013-02-02 20:50:49

标签: java java.util.scanner

在用Scanner(System.in)读取键盘输入时,程序在输入的最后一行停止,只有在输入控制台中按 enter 后才能继续工作。< / p>

例如,如果我想读这个

3
f
g
h

相反,我得到了

3
f
g

h

我的程序很简单,

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    int count = sc.nextInt();
    sc.nextLine();
    System.out.println("\n"+count);
    while(count>0) {
        System.out.println(sc.nextLine());
        count--;

    }
}

我无法理解发生了什么。我会帮助你。

1 个答案:

答案 0 :(得分:0)

我有一段时间没有使用过扫描仪,但我希望这有用:

String string;
java.util.Scanner mainScanner = new java.util.Scanner(System.in);
while( mainScanner.hasNextLine() )
{
    string = mainScanner.nextLine();
    // use string
}