Hi all,
I'm supposed to write a small program that asks the user for a number of a month( 1= Jan, 2=Feb, etc) and then outputs the month name and the month number. If the number entered is invalid (for example, > 12 or equal 0), then the user receives an error message. Perhaps there is a better way to write this, but based on my limited knowledge, this is what I have put together. However, when I run the program it prints ALL the months of the year, and then the single number input, like this:
Give me a number
10
January10
February 10
March10
April10
May10
June10
July10
August10
September10
Can anyone tell me what I've done incorrectly?
Here is the code:
import java.util.Scanner;
public class Exercise3 {
public static void main (String [] args) {
Scanner scanner = new Scanner(System.in);
{
System.out.println("Give me a number");
}
int x = scanner.nextInt();
while(x == 1);
{
System.out.println("January" + x);
}
while(x == 2);
{
System.out.println("February " + x);
}
while(x == 3);
{
System.out.println("March" + x);
}
while(x == 4);
{
System.out.println("April" + x);
}
while(x == 5);
{
System.out.println("May" + x);
}
while(x == 6);
{
System.out.println("June" + x);
}
while(x == 7);
{
System.out.println("July" + x);
}
while(x == 8);
{
System.out.println("August" + x);
}
while(x == 9);
{
System.out.println("September" + x);
}
while(x == 10);
{
System.out.println("October" + x);
}
while(x == 11);
{
System.out.println("November" + x);
}
while(x == 12);
{
System.out.println("December" + x);
}
while ((x > 12) || (x == 0));
{
System.out.println("Error. Number must be 12 or less");
}
}
}