我已经为硬币计数器的菜单系统创建了代码,我正在尝试使它成为可能,以便用户在选择第一个选项之后可以选择多个选项,直到决定结束程序为止。它不会循环,并且代码仅运行一个选项,然后我必须通过在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);
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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句