这个菜单哪里不对

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

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

  1. Scanner ss=new Scanner(System.in);
  2. int Choice;
  3. int opt;
  4. System.out.println("***Coin Sorter - Main Menu***");
  5. System.out.println(" 1 - Coin Calculator");
  6. System.out.println(" 2 - Multiple coin calculator");
  7. System.out.println(" 3 - Print Coin list");
  8. System.out.println(" 4 - Set details");
  9. System.out.println(" 5 - Display program configurations");
  10. System.out.println(" 6 - Quit the program");
  11. Choice=ss.nextInt();
  12. switch(Choice) {
  13. case 1: coinCalculator();
  14. break;
  15. case 2: multiCoinCalculator();
  16. break;
  17. case 3: printCoinList();
  18. break;
  19. case 4: System.out.println("***Set Details Sub-Menu***");
  20. System.out.println("1 - Set currency");
  21. System.out.println("2 - Set minimum coin input value");
  22. System.out.println("3 - Set maximum coin input value");
  23. System.out.println("4 - Return to main menu");
  24. opt=ss.nextInt();
  25. if (opt==1) {
  26. setCurrency(Currency);
  27. }else if (opt==2) {
  28. setMinCoinin(opt);
  29. }else if (opt==3) {
  30. setMaxCoinin(opt);
  31. }else if (opt==4);
  32. CoinSorter();
  33. break;
  34. case 5: displayProgramConfigs();
  35. break;
  36. case 6: if(Choice != 6) System.out.println("Unkown option");
  37. } while (Choice !=6);
ct3nt3jp

ct3nt3jp1#

while循环运行无限,必须使用do while来解决问题。

  1. Scanner ss=new Scanner(System.in);
  2. int Choice;
  3. int opt;
  4. do {
  5. System.out.println("***Coin Sorter - Main Menu***");
  6. System.out.println(" 1 - Coin Calculator");
  7. System.out.println(" 2 - Multiple coin calculator");
  8. System.out.println(" 3 - Print Coin list");
  9. System.out.println(" 4 - Set details");
  10. System.out.println(" 5 - Display program configurations");
  11. System.out.println(" 6 - Quit the program");
  12. Choice=ss.nextInt();
  13. switch(Choice) {
  14. case 1: coinCalculator();
  15. break;
  16. case 2: multiCoinCalculator();
  17. break;
  18. case 3: printCoinList();
  19. break;
  20. case 4: System.out.println("***Set Details Sub-Menu***");
  21. System.out.println("1 - Set currency");
  22. System.out.println("2 - Set minimum coin input value");
  23. System.out.println("3 - Set maximum coin input value");
  24. System.out.println("4 - Return to main menu");
  25. opt=ss.nextInt();
  26. if (opt==1) {
  27. setCurrency(Currency);
  28. }else if (opt==2) {
  29. setMinCoinin(opt);
  30. }else if (opt==3) {
  31. setMaxCoinin(opt);
  32. }else if (opt==4);
  33. CoinSorter();
  34. break;
  35. case 5: displayProgramConfigs();
  36. break;
  37. case 6: if(Choice != 6) System.out.println("Unkown option");
  38. }
  39. } while (Choice !=6);

它将正常运行。

展开查看全部
xxb16uws

xxb16uws2#

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

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner ss = new Scanner(System.in);
  5. int choice;
  6. int opt;
  7. do {
  8. System.out.println("***Coin Sorter - Main Menu***");
  9. System.out.println(" 1 - Coin Calculator");
  10. System.out.println(" 2 - Multiple coin calculator");
  11. System.out.println(" 3 - Print Coin list");
  12. System.out.println(" 4 - Set details");
  13. System.out.println(" 5 - Display program configurations");
  14. System.out.println(" 6 - Quit the program");
  15. choice = ss.nextInt();
  16. switch (choice) {
  17. case 1:
  18. coinCalculator();
  19. break;
  20. case 2:
  21. multiCoinCalculator();
  22. break;
  23. case 3:
  24. printCoinList();
  25. break;
  26. case 4:
  27. System.out.println("***Set Details Sub-Menu***");
  28. System.out.println("1 - Set currency");
  29. System.out.println("2 - Set minimum coin input value");
  30. System.out.println("3 - Set maximum coin input value");
  31. System.out.println("4 - Return to main menu");
  32. opt = ss.nextInt();
  33. if (opt == 1) {
  34. setCurrency(Currency);
  35. } else if (opt == 2) {
  36. setMinCoinin(opt);
  37. } else if (opt == 3) {
  38. setMaxCoinin(opt);
  39. } else if (opt == 4) {
  40. CoinSorter();
  41. }
  42. break;
  43. case 5:
  44. displayProgramConfigs();
  45. break;
  46. case 6:
  47. System.out.println("Good Bye!");
  48. break;
  49. default:
  50. System.out.println("Unkown option");
  51. }
  52. } while (choice != 6);
  53. }
  54. }
展开查看全部

相关问题