public class DisplayMonth
{
public static boolean validDay = false;
public static boolean validMonth = false;
public static int day = 0;
public static int month = 0;
public static void main(String[] args)
{
pickDay();
// System.out.println(day);
pickMonth();
//System.out.println(month);
}
public static int pickMonth()
{
System.out.println("1=Jan., 2=Feb., ..., 11=Nov., 12=Dec.");
System.out.print("Enter the month [1-12] ");
while (validMonth == false)
{
Scanner scan2 = new Scanner(System.in);
scan2.hasNext();
if (scan2.hasNext() != scan2.hasNextInt())
{
System.out.println("Error: Enter a value between 1 and 12 (Inclusive)");
}
if(scan2.hasNextInt())
{
int month = scan2.nextInt();
if (month == 1 || month == 2)
{
validMonth = true;
scan2.close();
}
else
{
System.out.println("Error: Enter a value between 1 and 12 (Inclusive)");
}
}
}
return month;
}
public static int pickDay()
{
System.out.println("1=Su., 2=Mo., 3=Tu., 4=We., 5=Th., 6=Fr., 7=Sa.");
System.out.print("Enter the day of the week [1-7] ");
while (validDay == false)
{
Scanner scan = new Scanner(System.in);
scan.hasNext();
if (scan.hasNext() != scan.hasNextInt())
{
System.out.println("Error: Enter a value between 1 and 7 (Inclusive)");
}
if(scan.hasNextInt())
{
int day = scan.nextInt();
if ((day == 1) || (day == 2) || (day == 3) || (day == 4) || (day == 5) || (day == 6) || (day == 7))
{
validDay = true;
scan.close();
System.out.println();
}
else
{
System.out.println("Error: Enter a value between 1 and 7 (Inclusive)");
}
}
}
return day;
}
}
The issue I am having with this code is that when method pickMonth runs it will keep asking for user input. When I put in 1 or 2 for the month it will stay inside my while loop but keep asking for user input instead of going to my if loops. I need the method to stop running once I type in 1 or 2 for when the user inputs those numbers.
Aucun commentaire:
Enregistrer un commentaire