我无法弄清楚如何将这个if语句放在我的while循环中

时间:2023-01-27 15:00:44

I'm just staring up coding and I was trying to make a code that would make the user have to guess the special day. It was working but I tried to make it more efficient by adding while loops to keep trying when the user fails rather than having to quit and restart. Instead of trying again, the code just ends when the user gets month 2 and a day above or under 18, here's the code:

我只是在盯着编码而我正在尝试制作一个能够让用户猜出特殊日子的代码。它工作但我试图通过添加while循环来提高效率,以便在用户失败时继续尝试而不必退出并重新启动。代码只是在用户获得第2个月以及18天之上或之后的一天时结束,而不是再次尝试,这里是代码:

import java.util.Scanner;

public class SpecialDay {
    public static void main(String[] args) {
        int Day, Month;

        Scanner scan = new Scanner(System.in);
        System.out.print("Welcom to the Special Day guessing game!");
        System.out.print(" Enter the Month: ");
        Month = scan.nextInt();
        System.out.print("Enter the Day: ");
        Day = scan.nextInt();

        while (Day != 18 && Month != 2) {
            System.out.print("Enter the Month: ");
            Month = scan.nextInt();
            System.out.print("Enter the Day: ");
            Day = scan.nextInt();
        }
        if (Month == 2 && Day == 18) {
            System.out.println("Nice! You got the Special Day!");
        } else if (Month >= 2 && Day > 18) {
            System.out.println("That's after the Special Day, try again!");
        } else if (Month <= 2 && Day < 18) {
            System.out.println("That's before the Special Day, try again!");
        }
    }
}

No hate please, I'm a newbie at this.

不讨厌,我是新手。

2 个解决方案

#1


Like Benjy Kessler said, the if statement needs to be in the while loop. I also fixed your if logic as well.

就像Benjy Kessler所说的那样,if语句需要在while循环中。我也修复了你的if逻辑。

while (Day != 18 || Month != 2)
{
    System.out.print ("Enter the Month: ");
    Month = scan.nextInt();
    System.out.print ("Enter the Day: ");
    Day = scan.nextInt();

    if (Month == 2 && Day == 18)
        System.out.println ("Nice! You got the Special Day!");
    else if ((Month > 2) || (Month == 2 && Day > 18))
        System.out.println ("That's after the Special Day, try again!");
    else if ((Month < 2) || (Month == 2 && Day < 18))
        System.out.println ("That's before the Special Day, try again!");
}

#2


The while should be like:

那应该是:

while ( (Day != 18 || Month != 2) && (Day>=1 && Day<=31 && Month>=1 && Month<=12){

...the code... }

...代码... }

And also can be used with do while like:

并且还可以与do同时使用:

do{
...the code...
}while ( (Day != 18 || Month != 2) && (Day>=1 && Day<=31 && Month>=1 && Month<=12);

And also can be written as a function:

并且还可以写成一个函数:

private boolean validateDayMonth(int Day, int Month){
    return ( (Day != 18 || Month != 2) && (Day>=1 && Day<=31 && Month>=1 && Month<=12);
}

#1


Like Benjy Kessler said, the if statement needs to be in the while loop. I also fixed your if logic as well.

就像Benjy Kessler所说的那样,if语句需要在while循环中。我也修复了你的if逻辑。

while (Day != 18 || Month != 2)
{
    System.out.print ("Enter the Month: ");
    Month = scan.nextInt();
    System.out.print ("Enter the Day: ");
    Day = scan.nextInt();

    if (Month == 2 && Day == 18)
        System.out.println ("Nice! You got the Special Day!");
    else if ((Month > 2) || (Month == 2 && Day > 18))
        System.out.println ("That's after the Special Day, try again!");
    else if ((Month < 2) || (Month == 2 && Day < 18))
        System.out.println ("That's before the Special Day, try again!");
}

#2


The while should be like:

那应该是:

while ( (Day != 18 || Month != 2) && (Day>=1 && Day<=31 && Month>=1 && Month<=12){

...the code... }

...代码... }

And also can be used with do while like:

并且还可以与do同时使用:

do{
...the code...
}while ( (Day != 18 || Month != 2) && (Day>=1 && Day<=31 && Month>=1 && Month<=12);

And also can be written as a function:

并且还可以写成一个函数:

private boolean validateDayMonth(int Day, int Month){
    return ( (Day != 18 || Month != 2) && (Day>=1 && Day<=31 && Month>=1 && Month<=12);
}