Explanation
1. Purpose
This program checks for an optional integer score from user input (defaulting to 82 if none is provided) and prints a feedback message based on that score.
2. Key Concepts
- User Input & Validation: Using
Scannermethods (hasNextInt()andnextInt()) to read input safely without crashing. - Default Values: Assigning a fallback value to a variable before checking for user input.
- Conditional Branching: Using an
if-else if-elseladder with relational operators (>=) to categorize values.
3. Code Walkthrough
- Lines 5–6 (Setup): Sets a default
score = 82and initializes aScannerto listen for user input from the console. - Lines 7–9 (Safe Input):
input.hasNextInt()checks if the user entered an integer. If yes,input.nextInt()updatesscore. If not (e.g., user enters text or presses EOF), the default score of82is preserved. - Lines 11–17 (Classification):
- If
score >= 85, prints"Excellent work". - Otherwise, if
score >= 70, prints"Good progress". - Otherwise (score is below 70), prints
"Keep practicing".
- If
4. Output
The output depends on the value entered:
- Default (no input provided) or input between
70and84:
Good progress(Note: Since the default is82, running without input triggers this) - Input $\ge 85$ (e.g.,
90):
Excellent work - Input $< 70$ (e.g.,
65):
Keep practicing