006-004-001
Do-While Loop: Menu Choice
Easy
Problem Description
Do-while loop: Menu Choice
In this problem, you will create a program that uses a do-while loop to display a "Menu choice (1-3):" prompt, repeatedly accept input until the user enters a valid choice (1–3), and displays the result to standard output.
Learning Objective: Learn processing executed at least once with do-while loop
Create a program that repeatedly displays a menu until a valid choice (1, 2, 3) is selected. Use a do-while loop to display the menu at least once.
Input
Multiple lines: Choices (integer, 1-3 correct, last is correct)
Output
Menu choice (1-3): [input1]
Invalid choice
Menu choice (1-3): [input2]
Selected: [input2]
Examples
Example 1: Invalid → Valid
Input:
5
2
Output:
Menu choice (1-3): 5
Invalid choice
Menu choice (1-3): 2
Selected: 2
Example 2: Valid from start
Input:
1
Output:
Menu choice (1-3): 1
Selected: 1
Example 3: Multiple invalid → Valid
Input:
0
4
3
Output:
Menu choice (1-3): 0
Invalid choice
Menu choice (1-3): 4
Invalid choice
Menu choice (1-3): 3
Selected: 3
Test Cases
※ Output examples follow programming industry standards
Normal case
Input:
2
Expected Output:
Menu choice (1-3): Selected: 2
Normal case
Input:
6 2
Expected Output:
Menu choice (1-3): Invalid choice Menu choice (1-3): Selected: 2
Your Solution
Current Mode:● My Code
99
1
2
3
4
5
6
7
8
9
10
›
⌄
⌄
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Write your code here
sc.close();
}
}
0 B / 5 MB
You have 10 free executions remaining
