dimanche 3 mai 2015

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

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:

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.

Aucun commentaire:

Enregistrer un commentaire