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

  1. Greet

  2. Tell a joke

  3. Show a random number (1–100)

  4. Play a number-guessing game

  5. Exit

Requirements

In option 4, start a number-guessing game:

  • Use Random to 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

  1. Greet

  2. Tell a joke

  3. Show a random number (1–100)

  4. Play a number-guessing game

  5. Exit

Requirements

In option 4, start a number-guessing game:

  • Use Random to 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();
    }
}
Project: Level Up the Java Menu App