这个菜单哪里出错了

Where am I going wrong with this menu

我已经为我的硬币计数器创建了一个菜单系统代码,我正在努力做到这一点,以便用户在选择第一个选项后可以选择多个选项,直到他们决定结束程序。它不会循环,代码只有 运行 一个选项,然后我必须通过 运行 在 eclipse 上手动重新启动代码。任何人都可以让我了解我在这里错过了什么,谢谢:)

Scanner ss=new Scanner(System.in);
int Choice;
int opt;
    System.out.println("***Coin Sorter - Main Menu***");
    System.out.println("    1 - Coin Calculator");
    System.out.println("    2 - Multiple coin calculator");
    System.out.println("    3 - Print Coin list");
    System.out.println("    4 - Set details");
    System.out.println("    5 - Display program configurations");
    System.out.println("    6 - Quit the program");
    Choice=ss.nextInt();
    switch(Choice) {
        case 1: coinCalculator();
                break;
        case 2: multiCoinCalculator();
                break;
        case 3: printCoinList();
                break;
        case 4: System.out.println("***Set Details Sub-Menu***");
                System.out.println("1 - Set currency");
                System.out.println("2 - Set minimum coin input value");
                System.out.println("3 - Set maximum coin input value");
                System.out.println("4 - Return to main menu");
                opt=ss.nextInt();
                if (opt==1) {
                setCurrency(Currency);
                }else if (opt==2) {
                    setMinCoinin(opt);
                }else if (opt==3) {
                    setMaxCoinin(opt);
                }else if (opt==4);
                    CoinSorter();
                break;
        case 5: displayProgramConfigs();
                break;
        case 6: if(Choice != 6) System.out.println("Unkown option");    
    } while (Choice !=6);

你的 while 循环是 运行 无限的,你必须使用 do-while 来解决这个问题。

Scanner ss=new Scanner(System.in);
int Choice;
int opt;
do {
    System.out.println("***Coin Sorter - Main Menu***");
    System.out.println("    1 - Coin Calculator");
    System.out.println("    2 - Multiple coin calculator");
    System.out.println("    3 - Print Coin list");
    System.out.println("    4 - Set details");
    System.out.println("    5 - Display program configurations");
    System.out.println("    6 - Quit the program");
    Choice=ss.nextInt();
    switch(Choice) {
        case 1: coinCalculator();
                break;
        case 2: multiCoinCalculator();
                break;
        case 3: printCoinList();
                break;
        case 4: System.out.println("***Set Details Sub-Menu***");
                System.out.println("1 - Set currency");
                System.out.println("2 - Set minimum coin input value");
                System.out.println("3 - Set maximum coin input value");
                System.out.println("4 - Return to main menu");
                opt=ss.nextInt();
                if (opt==1) {
                    setCurrency(Currency);
                }else if (opt==2) {
                    setMinCoinin(opt);
                }else if (opt==3) {
                    setMaxCoinin(opt);
                }else if (opt==4);
                    CoinSorter();
                break;
        case 5: displayProgramConfigs();
                break;
        case 6: if(Choice != 6) System.out.println("Unkown option");    
    } 

} while (Choice !=6);

它将 运行 正确。

  1. Unkown option 使用 default。从教程中了解更多信息,The switch Statement
  2. 将菜单正确地放在 do-while 循环中。您的代码中遗漏了 do
  3. 您在 else if (opt==4)
  4. 之后错误地添加了 ;

最后但同样重要的是,始终遵循 Java naming conventions 例如变量的名称可以是 choice 但不应该是 Choice.

演示:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner ss = new Scanner(System.in);
        int choice;
        int opt;
        do {
            System.out.println("***Coin Sorter - Main Menu***");
            System.out.println("    1 - Coin Calculator");
            System.out.println("    2 - Multiple coin calculator");
            System.out.println("    3 - Print Coin list");
            System.out.println("    4 - Set details");
            System.out.println("    5 - Display program configurations");
            System.out.println("    6 - Quit the program");
            choice = ss.nextInt();
            switch (choice) {
            case 1:
                coinCalculator();
                break;
            case 2:
                multiCoinCalculator();
                break;
            case 3:
                printCoinList();
                break;
            case 4:
                System.out.println("***Set Details Sub-Menu***");
                System.out.println("1 - Set currency");
                System.out.println("2 - Set minimum coin input value");
                System.out.println("3 - Set maximum coin input value");
                System.out.println("4 - Return to main menu");
                opt = ss.nextInt();
                if (opt == 1) {
                    setCurrency(Currency);
                } else if (opt == 2) {
                    setMinCoinin(opt);
                } else if (opt == 3) {
                    setMaxCoinin(opt);
                } else if (opt == 4) {
                    CoinSorter();
                }
                break;
            case 5:
                displayProgramConfigs();
                break;
            case 6:
                System.out.println("Good Bye!");
                break;
            default:
                System.out.println("Unkown option");
            }
        } while (choice != 6);
    }
}