case java code example
Example 1: java switch
int day = 4;
switch (day) {
case 6:
System.out.println("Today is Saturday");
break;
case 7:
System.out.println("Today is Sunday");
break;
default:
System.out.println("Looking forward to the Weekend");
}
Example 2: switch statement in java
public class SwitchStatementExample
{
public static void main(String[] args)
{
char grade = 'A';
switch(grade) {
case 'A' :
System.out.println("Distinction.");
break;
case 'B' :
case 'C' :
System.out.println("First class.");
break;
case 'D' :
System.out.println("You have passed.");
case 'F' :
System.out.println("Fail. Try again.");
break;
default :
System.out.println("Invalid grade");
}
System.out.println("Grade is: " + grade);
}
}
Example 3: java switch case
switch(x){
case(0):
break;
default:
break;
}
Example 4: switch statement in java
switch(expression)
{
case 1 value :
break;
case 2 value :
break;
case 3 value :
break;
.
.
.
.
default:
}
Example 5: switch case mit string
public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
String typeOfDay;
switch (dayOfWeekArg) {
case "Monday":
typeOfDay = "Start of work week";
break;
case "Tuesday":
case "Wednesday":
case "Thursday":
typeOfDay = "Midweek";
break;
case "Friday":
typeOfDay = "End of work week";
break;
case "Saturday":
case "Sunday":
typeOfDay = "Weekend";
break;
default:
throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
}
return typeOfDay;
}