building a Menu
How do you create a simple menu using a scanner and if statement
Make a selection:
- Print hello
- Print goodbye
- 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:
- Create a scanner
- Print the items on the screen
- Read the scanner input
- 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");
}
Seems like you already know how to "create a simple menu using a scanner and if statement"