Question 3
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
public class Main {
public static void main(String[] args) {
int limit = 10;
int value = 10;
switch (value) {
case limit: System.out.println("at limit"); break;
default: System.out.println("other");
}
}
}
- A value must be long
- B Case labels must be compile-time constants; limit is a non-final variable (making it final int limit = 10 would fix it) Correct
- C default must come first
- D switch cannot use int
Correct Answer: Option B
For changing values, use if/else.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!