這裏是我的代碼在做第一行代碼... 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) –