Search⌘ K

Solution Review: What Day Is It?

Explore how to use Java's switch statement to determine the day of the week based on numeric input. Learn to handle different cases for values 1 through 7, set appropriate day strings, and manage invalid inputs with a default case. This lesson reinforces effective use of conditional statements for more readable and maintainable code.

Solution: did you find the right day? #

Java
class HelloWorld {
public static void main( String args[] ) {
int x =3;
String answer = "";
switch (x) {
case 1:
answer = "Monday";
break;
case 2:
answer = "Tuesday";
break;
case 3:
answer = "Wednesday";
break;
case 4:
answer = "Thursday";
break;
case 5:
answer = "Friday";
break;
case 6:
answer = "Saturday";
break;
case 7:
answer = "Sunday";
break;
default:
answer = "invalid input";
}
System.out.println(answer);
}
}

Understanding the code

1. Line 5: ...