006-004-003

Do-While Loop: Menu Selection

Medium

Problem Description

Do-while loop: Menu Selection

Learning Objective: Implement loop that executes at least once using do-while

In this problem, you will create a program that displays a menu using a do-while loop, repeatedly accepts integer input until a valid number (1-3) is entered, and displays the selected result to standard output.

In the loop body of your do-while statement, display the menu and read an integer input. If the input is outside the range 1-3, execute the loop again. Once a valid value is entered, exit the loop and output the selection number.

Input

Multiple lines: Menu selection (integer, 1-3 valid, others cause re-input)

Output

Menu:
1. Start
2. Settings
3. Exit
[wait for input until first valid]
Selected: [selection number]

Test Cases

※ Output examples follow programming industry standards

Input:
2
Expected Output:
Menu:
1. Start
2. Settings
3. Exit
Selected: 2
Input:
5
1
Expected Output:
Menu:
1. Start
2. Settings
3. Exit
Selected: 1
Input:
3
Expected Output:
Menu:
1. Start
2. Settings
3. Exit
Selected: 3
Input:
0
0
2
Expected Output:
Menu:
1. Start
2. Settings
3. Exit
Selected: 2
❌ Some tests failed
❌ エラー発生

Your Solution

Current Mode: My Code
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 9 free executions remaining