Java 我将如何使用 while 循环来不断请求用户输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33036087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:12:46  来源:igfitidea点击:

How would I use a while loop to keep requesting user input

javaloopsinputwhile-loop

提问by Imminence

I've tried a couple of things with the while loop and can't seem to get it to work. I want to keep requesting user input until the user inputs the number 0, here is the code I have so far:

我已经用 while 循环尝试了几件事,但似乎无法让它工作。我想一直请求用户输入,直到用户输入数字 0,这是我到目前为止的代码:

import java.util.*;

public class Task10 {

    public static void main(String[] args) {
        System.out.println("Enter a year to check if it is a leap year");
        Scanner input = new Scanner(System.in);
        int year = input.nextInt();

        if ((year % 4 == 0) || ((year % 400 == 0) && (year % 100 != 0)))
            System.out.println(year + " is a leap year");
        else
            System.out.println(year + " is not a leap year");
    }
}

采纳答案by vish4071

Use a while loop above input line as:

在输入行上方使用 while 循环作为:

 while(true)

And, use ifcondition to break.

并且,使用if条件到break.

if(year == 0)
    break;

Also, condition for leap yearis wrong in your code. It should be:

此外,leap year您的代码中的条件错误。它应该是:

if((year % 100 == 0 && year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
    //its a leap year
else
    //its not

PS: As in comments, I'll give a complete code:

PS:在评论中,我会给出一个完整的代码:

import java.util.*;

public class Task10 {

public static void main(String[] args) {
    System.out.println("Enter a year to check if it is a leap year");
    while(true){
    Scanner input = new Scanner(System.in);
        int year = input.nextInt();
        if(year == 0)
            break;
        if((year % 100 == 0 && year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
            System.out.println(year + " is a leap year");
        else
            System.out.println(year + " is not a leap year");
    }
}

}

回答by NightsWatch

You should place your input taking code inside a while loop aned execute while loop untill year is 0 or lesser.

你应该把你的输入代码放在一个 while 循环中,然后执行 while 循环,直到 year 为 0 或更小。

public static void main(String[] args) {
        int year = 1;
        while(year > 0)
        {
            System.out.println("Enter a year to check if it is a leap year");
            Scanner input = new Scanner(System.in);
            year = input.nextInt();
            if ((year % 4 == 0) || ((year % 400 == 0) && (year % 100 != 0)))
                System.out.println(year + " is a leap year");
            else
                System.out.println(year + " is not a leap year");
        }


    }

回答by Saif Asif

You need to do something to keep you input loop running until a stopping condition is encountered (which in your case is that when the user inputs 0)

您需要做一些事情来保持输入循环运行,直到遇到停止条件(在您的情况下是用户输入时0

// First get the scanner object with the input stream
Scanner sc = new Scanner(System.in); 

// Just using do-while here for no reason, you can use a simple while(true) as well
do{
    int input = sc.nextInt();  // read the next input
    if (int == 0) { // check if we need to exit out
        // break only if 0 is entered, this means we don't want to run the loop anymore
        break;
    } else {
        // otherwise, do something with the input
    }
} while(true); // and keep repeating