这个菜单哪里不对

qgelzfjb  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(329)

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

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);
ct3nt3jp

ct3nt3jp1#

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);

它将正常运行。

xxb16uws

xxb16uws2#

使用 default 为了 Unkown option . 从教程switch语句了解更多信息
把菜单放在 do-while 正确循环。你错过了 do 在你的代码里。
你错放了一个 ; 之后 else if (opt==4) 最后但并非最不重要的一点是,始终遵循java命名约定,例如变量的名称可以是 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);
    }
}

相关问题