【问题标题】:Where am I going wrong with this menu这个菜单我哪里错了
【发布时间】:2021-03-04 08:33:36
【问题描述】:
我已经为我的硬币计数器创建了一个菜单系统的代码,我正在努力让它在用户选择第一个选项后可以选择多个选项,直到他们决定结束程序。它不会循环,代码只运行一个选项,然后我必须通过在 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);
【问题讨论】:
标签:
java
eclipse
loops
if-statement
switch-statement
【解决方案1】:
您的 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);
它会正确运行。
【解决方案2】:
- 将
default 用于Unkown option。从教程中了解更多信息,The switch Statement
- 将菜单正确放入
do-while 循环中。您在代码中错过了do。
- 您在
else if (opt==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);
}
}