Java Switch Statement Example: Difficulty Levels
This Snippet is coded by a user inOnline java Compiler. You can see and run this code too.
زبان: Java
This Java code demonstrates a simple switch statement that maps an integer level to a difficulty string. It initializes the variable level to 4 and uses a switch to check the value against several cases.
The switch statement is a control flow statement that allows a variable to be tested for equality against a list of values. Each case prints a corresponding difficulty:
- 1 → "veryeasy"
- 2 → "easy"
- 3 → "medium"
- 4 → "hard"
- default → "unknown"
This is a basic example useful for beginners learning Java syntax and decision-making structures.