Ask coding questions

← Back to all posts
building a Menu
joanna14 (1)

How do you create a simple menu using a scanner and if statement

Comments
hotnewtop
UniqueOstrich18 (331)

Seems like you already know how to "create a simple menu using a scanner and if statement"

joanna14 (1)

Make a selection:

1) Print hello
2) Print goodbye
3) Print three

After printing the menu, the program blocks and waits for the user to input a number. In order to make this happen we need the following to happen:

Print the items on the screen
Wait for the user input
Test the user input

If the user input is a 1, print hello
If the user input is a 2, print goodbye
If the user input is a 3, print three
If the user input is not one of these numbers, print invalid selection

So, our code is going to:

  1. Create a scanner
  2. Print the items on the screen
  3. Read the scanner input
  4. Use if statements to test the input and output the correct items.

Your assignment is to complete the code...

Hint - the following code is almost the if statements you need ... missing what happens if input == 3:

if (input == 1) {
System.out.println("hello");
} else if (input == 2) {
System.out.println("goodbye");

(put the an else if statement here - before the else - the else goes last)

} else {
System.out.println("invalid selection");
}