Simple Java Program 7: Choosing Your Year Level Using Switch

This program will ask the user to input his year level. If he presses 1, it will display "First year." If he presses 2, it will display "Second year." If he presses 3, it will display "Third year." If he presses 4, it will display "Fourth year." If he presses any other number above 4, it will display "Error." That's the function of "default"
When using switch, the default statement isn't required. It is only optional.

import java.util.Scanner;
public class YrLvl {

public static void main(String[] args) {
int a;
Scanner reader = new Scanner(System.in);
System.out.println("Enter your year level: ");
a = reader.nextInt();
switch (a)
{
case 1: System.out.println("First Year"); break;
case 2: System.out.println("Second Year"); break;
case 3: System.out.println("Third Year"); break;
case 4: System.out.println("Fourth Year"); break;
default: System.out.println("Error");
}


}

}

Here's the output:

Comments