【问题标题】:How would I use a while loop to keep requesting user input我将如何使用 while 循环来继续请求用户输入
【发布时间】:2016-01-07 06:34:12
【问题描述】:

我用 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");
    }
}

【问题讨论】:

  • 你的闰年逻辑错了。

标签: java loops input while-loop


【解决方案1】:

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

 while(true)

并且,使用if 条件到break

if(year == 0)
    break;

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

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

PS:和cmets一样,我给出一个完整的代码:

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");
    }
}

}

【讨论】:

  • 谢谢伙计,很有魅力。仍然习惯了while循环,这个解释陷入了困境!
【解决方案2】:

您需要做一些事情来保持输入循环运行,直到遇到停止条件(在您的情况下,当用户输入 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

【讨论】:

    【解决方案3】:

    您应该将输入代码放在一个while循环中并执行while循环,直到年份为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");
            }
    
    
        }
    

    【讨论】:

    • while 循环之外从不需要它时,为什么要将year 变量的范围设为main 的局部变量?!
    • @vish4071 它减少了需要编写的代码行数,而不是首先将 while 标记为 true,然后检查条件如果 year 为 0 以打破循环,我觉得我可以制作初学者这样理解更好
    • 这是不好的做法。您应该尝试声明/定义仅在需要时使用的变量。让它rule of the thumb。一些 LoC 对项目没有影响,但是,变量和内存等的范围有很大的不同。
    • 不过既然是小程序,那就无所谓了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 2020-05-24
    • 1970-01-01
    • 2013-11-16
    • 2020-02-15
    • 1970-01-01
    相关资源
    最近更新 更多