
How we can use switch case in java program ?

Note:-Switch case provides built-in multiway decision statement.It switches to statement by testing the value.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SwitchTest {
public static void main(String [] args) throws IOException{
int days= 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter The Number");
String data = br.readLine();
days= Integer.parseInt(data);
switch(days){
case 1: System.out.println("Sunday");
break;
case 2: System.out.println("Monday");
break;
case 3: System.out.println("Tuesday");
break;
case 4: System.out.println("Wednesday");
break;
case 5: System.out.println("Thursday");
break;
case 6: System.out.println("Friday");
break;
case 7: System.out.println("Saturday");
break;
default: System.out.println("Invalid days.");
break;
}
}
}
Output
Enter the number 3 Tuesday
Description: - In this example, we are using switch case statement. The program displays the name of the days according to user choice. As we inputted 3 so it switches to the case 3 and prints the value of case 3.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.