switch语句在java计算器中的连续性问题

mefy6pfw  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(337)

我是java新手,我正在做一个计算器应用程序。
我试图让我的计算器在“invalid input”后面的else语句之后不断提示用户输入正确答案(键入y或n)。
我希望程序在最终输入正确的输入后继续计算。
我玩过一个嵌入式游戏 while 循环,但最终得到一个无限循环或一个终止时没有分辨率的循环。代码如下。

  1. import java.util.Scanner;
  2. class Calculate {
  3. public static void.main(String[] args) {
  4. System.out.println("Welcome To Calculator!");
  5. System.out.println("*************************************************************************");
  6. Scanner userinput = new Scanner(System.in);
  7. double num1, num2;
  8. String choice;
  9. boolean youDecide = true;
  10. while(youDecide == true) {
  11. System.out.println("Please enter a number: ");
  12. num1 = userinput.nextDouble();
  13. System.out.println("Please enter an available operator (+, -, *, /): ");
  14. char operator = userinput.next().charAt(0);
  15. System.out.println("Please enter another number: ");
  16. num2 = userinput.nextDouble();
  17. double output;
  18. switch(operator)
  19. {
  20. case '+':
  21. output = num1 + num2;
  22. break;
  23. case '-':
  24. output = num1 - num2;
  25. break;
  26. case '*':
  27. output = num1 * num2;
  28. break;
  29. case '/':
  30. output = num1 / num2;
  31. if(num2 == 0)
  32. System.out.println("Math error! A number cannot be divided by zero.");
  33. break;
  34. default:
  35. System.out.println("Invalid input. Please enter an available operator, i.e (+, -, *, /): ");
  36. return;
  37. }
  38. System.out.println("*************************************************************************");
  39. System.out.println("The answer is: " + "\n" + output);
  40. System.out.println("*************************************************************************");
  41. System.out.println("Would you like to calculate again?");
  42. System.out.println("Please enter Y for yes, or N for no");
  43. choice = userinput.next();
  44. if(choice.equalsIgnoreCase("Y")) {
  45. System.out.println("Okay. Let's continue!");
  46. System.out.println("*************************************************************************");
  47. youDecide = true;
  48. }
  49. else if(choice.equalsIgnoreCase("N")) {
  50. System.out.println("*************************************************************************");
  51. System.out.println("Okay. Thanks for using Calculator. Goodbye!");
  52. System.exit(0);
  53. }
  54. else {
  55. System.out.println("Invalid input. Please try again...");
  56. System.out.println("*************************************************************************");
  57. youDecide = false;
  58. }
  59. }
  60. }
  61. }
ubbxdtey

ubbxdtey1#

我对你的代码做了一些修改和注解

  1. import java.util.Scanner;
  2. class Calculator {
  3. public static void main(String[] args) {
  4. System.out.println("Welcome To Calculator!");
  5. System.out.println("*************************************************************************");
  6. Scanner userinput = new Scanner(System.in);
  7. double num1, num2;
  8. String choice;
  9. //the "youDecide" variable is not needed at all
  10. while (true) {
  11. System.out.println("Please enter a number: ");
  12. num1 = userinput.nextDouble();
  13. System.out.println("Please enter an available operator (+, -, *, /): ");
  14. char operator = userinput.next().charAt(0);
  15. System.out.println("Please enter another number: ");
  16. num2 = userinput.nextDouble();
  17. double output;
  18. switch (operator) {
  19. case '+':
  20. output = num1 + num2;
  21. break;
  22. case '-':
  23. output = num1 - num2;
  24. break;
  25. case '*':
  26. output = num1 * num2;
  27. break;
  28. case '/':
  29. output = num1 / num2;
  30. if (num2 == 0) {
  31. System.out.println("Math error! A number cannot be divided by zero.");
  32. }
  33. break;
  34. default:
  35. System.out.println("Invalid input. Please enter an available operator, i.e (+, -, *, /): ");
  36. continue; //changed from "return" you don't want to exit, just to skip to the next execution of the loop
  37. }
  38. System.out.println("*************************************************************************");
  39. System.out.println("The answer is: " + "\n" + output);
  40. System.out.println("*************************************************************************");
  41. System.out.println("Would you like to calculate again?");
  42. System.out.println("Please enter Y for yes, or N for no");
  43. choice = userinput.next();
  44. if (choice.equalsIgnoreCase("Y")) {
  45. System.out.println("Okay. Let's continue!");
  46. System.out.println("*************************************************************************");
  47. } else if (choice.equalsIgnoreCase("N")) {
  48. System.out.println("*************************************************************************");
  49. System.out.println("Okay. Thanks for using Calculator. Goodbye!");
  50. System.exit(0);
  51. } else {
  52. System.out.println("Invalid input. Please try again...");
  53. System.out.println("*************************************************************************");
  54. }
  55. }
  56. }
  57. }
展开查看全部
kcugc4gi

kcugc4gi2#

你需要一个 while 循环以确保值正确。
由于代码中的逻辑,当用户键入char时需要执行此操作,否则必须更改许多行代码,或者再次请求所有值。
我认为对于乞丐来说,最简单的方法就是使用这个循环:

  1. char operator;
  2. while(true) {
  3. operator = userinput.next().charAt(0);
  4. if(operator=='+' || operator == '-' || operator == '*' || operator == '/') {
  5. break;
  6. }else {
  7. System.out.println("Please enter a valid operator: ");
  8. }
  9. }

有很多方法可以做到这一点。但我认为对于乞丐来说,理解和实现代码是最简单的方法。
此循环仅用于确保用户键入有效字符。当字符不是其中之一时,循环将进行迭代。
您必须将此代码放置在要求有效运算符的此行下方。

展开查看全部
f87krz0w

f87krz0w3#

在添加j.f.和javaman建议的编辑以及一些研究之后,我能够通过在代码中添加以下行来解决问题:

  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3. class Calculator {
  4. public static void main(String[] args) {
  5. System.out.println("Welcome To Calculator!");
  6. System.out.println("*************************************************************************");
  7. Scanner userinput = new Scanner(System.in);
  8. char operator;
  9. double num1, num2;
  10. String choice;
  11. while(true) {
  12. System.out.println("Please enter a number: ");
  13. num1 = userinput.nextDouble();
  14. System.out.println("Please enter an available operator (+, -, *, /): ");
  15. while(true) {
  16. operator = userinput.next().charAt(0);
  17. if(operator == '+' || operator == '-' || operator == '*' || operator == '/') {
  18. break;
  19. }else {
  20. System.out.println("Invalid input. Please enter an available operator, i.e (+, -, *, /): ");
  21. }
  22. }
  23. System.out.println("Please enter another number: ");
  24. num2 = userinput.nextDouble();
  25. double output;
  26. switch(operator) {
  27. case '+':
  28. output = num1 + num2;
  29. break;
  30. case '-':
  31. output = num1 - num2;
  32. break;
  33. case '*':
  34. output = num1 * num2;
  35. break;
  36. case '/':
  37. output = num1 / num2;
  38. break;
  39. default:
  40. System.out.println("Invalid input. Please enter an available operator, i.e (+, -, *, /): ");
  41. continue;
  42. }
  43. System.out.println("*************************************************************************");
  44. if(num2 == 0) {
  45. System.out.println("Math error! A number cannot be divided by zero.");
  46. }else {
  47. System.out.println("The answer is: " + "\n" + output);
  48. }
  49. System.out.println("*************************************************************************");
  50. System.out.println("Would you like to calculate again?");
  51. System.out.println("Please enter Y for yes, or N for no");
  52. choice = userinput.next();
  53. if(choice.equalsIgnoreCase("Y")) {
  54. System.out.println("Okay. Let's continue!");
  55. System.out.println("*************************************************************************");
  56. }else if(choice.equalsIgnoreCase("N")) {
  57. System.out.println("*************************************************************************");
  58. System.out.println("Okay. Thanks for using Calculator. Goodbye!");
  59. System.exit(0);
  60. }else {
  61. System.out.println("Invalid input. Please try again...");
  62. System.out.println("*************************************************************************");
  63. **while (!("Y").equalsIgnoreCase(choice) && !("N").equalsIgnoreCase(choice)) {
  64. System.out.println("Invalid input. Please try again...");
  65. System.out.println("*************************************************************************");
  66. System.out.println("Please enter Y for yes, or N for no");
  67. choice = userinput.next();
  68. if(choice.equalsIgnoreCase("Y")) {
  69. System.out.println("Okay. Let's continue!");
  70. System.out.println("*************************************************************************");
  71. }else if(choice.equalsIgnoreCase("N")) {
  72. System.out.println("*************************************************************************");
  73. System.out.println("Okay. Thanks for using Calculator. Goodbye!");
  74. System.exit(0);**
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }
展开查看全部

相关问题