JAVA的main menu問題

我想Display一個Main Menu 係Option果到揀

好似係咁 用家除左ABCDQ abcdq唔可以打其它

想問下點整

Welcome to Math Quiz

==| Main Menu |==

A ) Square Root

B ) Maximun Prime Number

C ) Highest Common Factor (HCF)

D ) Random Questions in (A) - (C)

Q ) Quit

Option:__

1 個解答

評分
  • John
    Lv 7
    6 年前
    最愛解答

    import java.util.Scanner;

    public class Program {

    public static void main(String[] args) {

    boolean done = false;

    String option = null;

    Scanner kb = new Scanner(System.in);

    do {

    System.out.println(showMenu());

    option = kb.nextLine();

    if (option.toLowerCase().matches("^[abcdq]{1}$")) {

    done = true;

    } else {

    System.err.println("Error: invalid option was entered");

    }

    } while (!done);

    System.out.printf("Your option is %s.%n", option);

    }

    private static String showMenu() {

    StringBuilder menu = new StringBuilder("Welcome to Math Quiz\n");

    menu.append("==| Main Menu |==\n");

    menu.append("A ) Square Root\n");

    menu.append("B ) Maximun Prime Number\n");

    menu.append("C ) Highest Common Factor (HCF)\n");

    menu.append("D ) Random Questions in (A) - (C)\n");

    menu.append("Q ) Quit\n");

    menu.append("Option:");

    return menu.toString();

    }

    }

還有問題嗎?立即提問即可得到解答。