Question: Here is a Java statement switch ( color ) { case Color.RED: System.out.println ( stop ) ; case Color.YELLOW: System.out.println ( slow

Here is a Java statement
switch (color){
case Color.RED: System.out.println("stop");
case Color.YELLOW: System.out.println("slow down");
case Color.GREEN: System.out.println("go");
}
Color is an enum with three variants. This code will actually run, but it doesn't do what is intended. Which of the following fix the problem?
when (color){
| Color.RED: System.out.println("stop");
| Color.YELLOW: System.out.println("slow down");
| Color:GREEN: System.out.println("go");
}
String action = switch (color){
case Color.RED -> "stop";
case Color.YELLOW -> "slow down";
case Color.GREEN ->"go";
}
switch (color){
case Color.RED -> System.out.println("stop");
case Color.YELLOW -> System.out.println("slow down");
default -> System.out.println("go");
}
var action = switch (color){
case Color.RED -> "stop";
case Color.YELLOW -> "slow down";
case Color.GREEN ->"go";
}
System.out.println(action);
System.out.println(switch (color){
case Color.RED -> "stop";
case Color.YELLOW -> "slow down";
case Color.GREEN ->"go";
});

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!