这里是我的代码在做第一行代码... while循环跳过而进入第二次循环时 - 的Java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] data = new int[10];
int count = 0;
do {
System.out.print("Enter a number or ctrl + z when you are done: ");
data[count] = input.nextInt();
}
while (input.hasNextDouble());
}
}
输出
Enter a number or ctrl + z when you are done: 2
4
Enter a number or ctrl + z when you are done: 6
Enter a number or ctrl + z when you are done: 8
我的问题是,我不知道为什么代码在第二次进入循环时跳过System.out.print("Enter a number or ctrl + z when you are done: ");
后do {
。这可以在输出的第二行看到。请问我做错了什么?
...我已经寻找情况下,我的问题可能已经回答了,但只能够找到与代码解决方案跳过nextLine()
为什么在阅读整数时检查流中是否存在另一个双精度值? – tkausl
[while hasNextInt()While while actually reading?](https://stackoverflow.com/questions/35633931/while-loop-with-hasnextint-actually-reading) –