扫描器Java的问题
我正在编写一个程序,需要收集一个数字块并对其进行处理。但由于某些原因,扫描仪无法读取所有输入的数字。怎么了扫描器Java的问题,java,Java,我正在编写一个程序,需要收集一个数字块并对其进行处理。但由于某些原因,扫描仪无法读取所有输入的数字。怎么了 int i = 0; while (i >=0){ System.out.println(sc.nextInt()); i++; } 输入: 3 100 3 5 75 25 200 7 150 24 79 50 88 345 3 8 8 2 1 9 4 4 56 90 3 3 100 3 5 75 25 200 7 150 24 79 50 88 345 3 8
int i = 0;
while (i >=0){
System.out.println(sc.nextInt());
i++;
}
输入:
3
100
3
5 75 25
200
7
150 24 79 50 88 345 3
8
8
2 1 9 4 4 56 90 3
3
100
3
5
75
25
200
7
150
24
79
50
88
345
3
8
8
输出:
3
100
3
5 75 25
200
7
150 24 79 50 88 345 3
8
8
2 1 9 4 4 56 90 3
3
100
3
5
75
25
200
7
150
24
79
50
88
345
3
8
8
它缺少最后8位数字。这是为什么?从System.in进行扫描要求用户在返回下一个扫描的整数之前按Enter键。对于仅从用户处获取整数,在用户在此代码中输入字符或除整数以外的任何内容之前,扫描仪将只工作。
try {
ArrayList<Integer> ar=new ArrayList<Integer>();
Scanner s = new Scanner(System.in);
System.out.println("ENTER ANY CHARACTER AND PRESS ENTER TO EXIT!!");
while(s.hasNextInt())
{
ar.add(s.nextInt());
}
for(int i:ar)
{
System.out.println(i);
}
} catch (Exception e) {
e.printStackTrace();
}
试试看{
ArrayList ar=新的ArrayList();
扫描仪s=新的扫描仪(System.in);
System.out.println(“输入任意字符并按ENTER键退出!!”;
而(s.hasnetint())
{
ar.add(s.nextInt());
}
for(int i:ar)
{
系统输出打印LN(i);
}
}捕获(例外e){
e、 printStackTrace();
}
是否需要用户按返回键?您确定您向我们显示了正确的代码和输入吗?@Jason否,只是自动显示output@SotiriosDelimanolis是的,先生。你能分享你的实际代码吗?这和最初的问题有什么关系?