Challenge: Level Up the Java Menu App
You’ve built a menu app that greets users and tells jokes. Now let’s make it smarter and more interactive.
Add new menu options: Show a random number and play a number guessing game.
Menu options
Greet
Tell a joke
Show a random number (1–100)
Play a number-guessing game
Exit
Requirements
In option 4, start a number-guessing game:
Use
Randomto generate a number between 1 and 10.Ask the user to guess the number until they get it right.
After each guess, say “Too high,” “Too low,” or “Correct!”.
Return to the main menu once they guess correctly.
Challenge: Level Up the Java Menu App
You’ve built a menu app that greets users and tells jokes. Now let’s make it smarter and more interactive.
Add new menu options: Show a random number and play a number guessing game.
Menu options
Greet
Tell a joke
Show a random number (1–100)
Play a number-guessing game
Exit
Requirements
In option 4, start a number-guessing game:
Use
Randomto generate a number between 1 and 10.Ask the user to guess the number until they get it right.
After each guess, say “Too high,” “Too low,” or “Correct!”.
Return to the main menu once they guess correctly.
import java.util.Scanner;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
int choice = 0;
while (choice != 5) {
System.out.println("\nMain Menu");
System.out.println("1. Greet");
System.out.println("2. Tell a joke");
System.out.println("3. Show a random number (1–100)");
System.out.println("4. Play the guessing game");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
// TODO: Read input, use switch-case to handle each option
}
input.close();
}
}