简体   繁体   English

Do-While循环和hasNextInt()

[英]Do-While loop and hasNextInt()

I know this might sound like a really stupid question but I cannot understand where is my mistake. 我知道这可能听起来像一个非常愚蠢的问题,但我无法理解我的错误在哪里。

Why on the second iteration of the loop, it does not print 'Enter a number:'? 为什么在循环的第二次迭代中,它不会打印'输入数字:'?

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner console = new Scanner(System.in);
        int[] v = new int[10];
        int index = 0;
        do {
            System.out.print("Enter a number:\t");
            v[index] = console.nextInt();
            index++;
        } while(console.hasNextInt());

        for (int i = 0; i < index; i++){
            System.out.print(v[i] + "\t");
        }
        System.out.println("\n" + index);
    }
}

And this is the output: 这是输出:

Enter a number: 1
2
Enter a number: 3
Enter a number: 4
Enter a number: 5
Enter a number: ^D
1       2       3       4       5
5

因为hasNextInt阻塞,直到控制台上有一个int ,因此不会进入循环的下一次迭代。

Welcome. 欢迎。 See answer in comments. 请参阅评论中的答案。 Hope it is clear :-) 希望很清楚:-)

do {
        System.out.print("Enter a number:\t");  // Prints "Enter a number: "
        v[index] = console.nextInt(); // accepts input "1"
        index++; // increments
    } while(console.hasNextInt()); // waits for input at which point you enter "2"

Ok Let's make it clearer. 好的,让我们更清楚。

The do while loop, executes the do block before it evaluates the while condition. do while循环在评估while条件之前执行do块。 And then if the while condition evaluates to true, it executes the do block again and repeats until the while condition evaluates to false. 然后,如果while条件的计算结果为true,它将再次执行do块并重复,直到while条件的计算结果为false。 .

Both console.nextInt and console.hasNextInt read input from the console. console.nextInt和console.hasNextInt都从控制台读取输入。 So as part of the do block, the "Enter a number:\\t" has been printed out, The first nextInt() call has accepted the input "1", which is then followed by the increment, followed by evaluation of the while condition - the console.hasNextInt(), which again waits for input and accepts "2". 因此作为do块的一部分,打印出“输入数字:\\ t”,第一个nextInt()调用已接受输入“1”,然后是增量,然后评估while condition - console.hasNextInt(),它再次等待输入并接受“2”。 This explains why the "Enter a number:\\t" was not printed before the User Input of "2" 这解释了为什么在用户输入“2”之前没有打印“输入数字:\\ t”

Of course because the value 2 has been entered, the while condition evaluates to true and again the do block is executed and goes on. 当然因为输入了值2,所以while条件的计算结果为true,并且do块再次执行并继续。

Perhaps you need the while loop. 也许你需要while循环。 This, by contrast, executes the code block only if and as long as the while condition evaluates to true 相反,只有当while条件的计算结果为true时,才会执行此代码块

System.out.print("Enter a number:\t");
while(console.hasNextInt()){
        v[index] = console.nextInt(); 
        index++; // increments
        System.out.print("Enter a number:\t");  
    } 

Think of exiting the loop when you reached the enough size of the array. 考虑到达到足够大小的数组时退出循环。 something like 就像是

   do {
            System.out.print("Enter a number:\t");
            v[index] = console.nextInt();
            index++;
        } while(index<10); // if you want the user to enter 10 numbers. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM