So I want my program to read an input, which has some integers in one line, for example:
1 1 2
Then it should read every integer separately and print it in a new line. The number of integers the program has to read is not given in advance, so what I am trying to do is use a while loop, which ends after there are no more integers to read. This is the code I wrote:
while (scan.hasNextInt()) {
int x = scan.nextInt();
System.out.println(x);
}
but it's not working correctly, because the loop never ends, it just wants the user to input more integers. What am I missing here?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…