我不知道如何将这个 if 语句放入我的 while 循环中

I can't figure out how to put this if statement in my while loop

我刚刚开始编码,我正在尝试编写一个让用户不得不猜测特殊日子的代码。它正在工作,但我试图通过添加 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!");
        }
    }
}

请不要讨厌,我是这方面的新手。

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

while 应该是这样的:

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

...代码... }

也可以与 do while 一起使用,例如:

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

也可以写成函数:

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