如何在 Switch 语句中捕获用户输入的数字不存在的错误。

huangapple 未分类评论49阅读模式
英文:

How to catch an Error in a Switch statement when the user entered number doesn't exist

问题

我试图使我的程序防错,该程序基本上是一个迷你计算器。但我不知道如何编写一个“Catch”语句,以便在用户输入不存在的情况下(在我的情况下为任何负数或大于4的数字)进行检测。

  1. public static void main(String[] args) {
  2. System.out.println("你好用户!你想要使用哪种操作?");
  3. System.out.println("1) + \n2) - \n3) * \n4) /");
  4. Scanner operacijai = new Scanner(System.in);
  5. int operacija = operacijai.nextInt();
  6. int n = 1;
  7. do {
  8. try {
  9. switch (operacija) {
  10. case 1:
  11. addingMethod();
  12. n = 2;
  13. break;
  14. case 2:
  15. subtractingMethod();
  16. n = 2;
  17. break;
  18. case 3:
  19. multiplyingMethod();
  20. n = 2;
  21. break;
  22. case 4:
  23. dividingMethod();
  24. n = 2;
  25. break;
  26. }
  27. } catch (Exception e) {
  28. System.out.print("请输入正确的数字!");
  29. }
  30. } while (n == 1);
  31. operacijai.close();
  32. }
英文:

I'm trying to error proof my program that basically works as a mini calculator. But I have no idea how to write a "Catch" statement that would detect when the user enters a case number that doesn't exist, in my case anything that is negative or > 4

  1. System.out.println("Hello user! Which operation would you like to use?");
  2. System.out.println("1) + \n2) - \n3) * \n4) /");
  3. Scanner operacijai = new Scanner(System.in);
  4. int operacija = operacijai.nextInt();
  5. int n=1;
  6. do {
  7. try {
  8. switch (operacija) {
  9. case 1:
  10. addingMethod();
  11. n=2;
  12. break;
  13. case 2:
  14. subtractingMethod();
  15. n=2;
  16. break;
  17. case 3:
  18. multiplyingMethod();
  19. n=2;
  20. break;
  21. case 4:
  22. dividingMethod();
  23. n=2;
  24. break;
  25. }
  26. }
  27. catch(Exception e) {
  28. System.out.print("Enter a correct number!");
  29. }
  30. } while(n==1);
  31. operacijai.close();
  32. } ```
  33. </details>
  34. # 答案1
  35. **得分**: 2
  36. 为什么你要不必要地抛出一个 `Exception`?我建议你在 `switch` 中加入一个带有所需错误消息的 `default` 情况。另外,将输入部分移动到循环内,这样它就会继续获取输入。
  37. 我还建议你使用 `nextLine()` 而不是 `nextInt()`。了解更多信息,请查看 https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo。
  38. ```java
  39. import java.util.Scanner;
  40. public class Main {
  41. public static void main(String[] args) {
  42. System.out.println("Hello user! Which operation would you like to use?");
  43. System.out.println("1) + \n2) - \n3) * \n4) /");
  44. Scanner operacijai = new Scanner(System.in);
  45. int operacija = 0, n = 1;
  46. boolean valid;
  47. do {
  48. do {
  49. valid = true;
  50. try {
  51. operacija = Integer.parseInt(operacijai.nextLine());
  52. } catch (NumberFormatException e) {
  53. System.out.println("Enter an integer only.");
  54. valid = false;
  55. }
  56. } while (!valid);
  57. switch (operacija) {
  58. case 1:
  59. System.out.println("addingMethod()");
  60. n = 2;
  61. break;
  62. case 2:
  63. System.out.println("subtractingMethod()");
  64. n = 2;
  65. break;
  66. case 3:
  67. System.out.println("multiplyingMethod()");
  68. n = 2;
  69. break;
  70. case 4:
  71. System.out.println("dividingMethod()");
  72. n = 2;
  73. break;
  74. default:
  75. System.out.println("Invalid input");
  76. }
  77. } while (n == 1);
  78. }
  79. }

一个示例运行:

  1. Hello user! Which operation would you like to use?
  2. 1) +
  3. 2) -
  4. 3) *
  5. 4) /
  6. 5
  7. Invalid input

另一个示例运行:

  1. Hello user! Which operation would you like to use?
  2. 1) +
  3. 2) -
  4. 3) *
  5. 4) /
  6. a
  7. Enter an integer only.
  8. 5
  9. Invalid input
  10. 2
  11. subtractingMethod()
英文:

Why do you want to throw an Exception unnecessarily? I suggest you just put a default case in your switch with the required error message. Also, move the input part inside the loop, so that it continues to take input.

I also suggest you use nextLine() instead of nextInt(). Check https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo to learn more about it.

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. System.out.println(&quot;Hello user! Which operation would you like to use?&quot;);
  5. System.out.println(&quot;1) + \n2) - \n3) * \n4) /&quot;);
  6. Scanner operacijai = new Scanner(System.in);
  7. int operacija = 0, n = 1;
  8. boolean valid;
  9. do {
  10. do {
  11. valid = true;
  12. try {
  13. operacija = Integer.parseInt(operacijai.nextLine());
  14. } catch (NumberFormatException e) {
  15. System.out.println(&quot;Enter an integer only.&quot;);
  16. valid = false;
  17. }
  18. } while (!valid);
  19. switch (operacija) {
  20. case 1:
  21. System.out.println(&quot;addingMethod()&quot;);
  22. n = 2;
  23. break;
  24. case 2:
  25. System.out.println(&quot;subtractingMethod()&quot;);
  26. n = 2;
  27. break;
  28. case 3:
  29. System.out.println(&quot;multiplyingMethod()&quot;);
  30. n = 2;
  31. break;
  32. case 4:
  33. System.out.println(&quot;dividingMethod()&quot;);
  34. n = 2;
  35. break;
  36. default:
  37. System.out.println(&quot;Invalid input&quot;);
  38. }
  39. } while (n == 1);
  40. }
  41. }

A sample run:

  1. Hello user! Which operation would you like to use?
  2. 1) +
  3. 2) -
  4. 3) *
  5. 4) /
  6. 5
  7. Invalid input

Another sample run:

  1. Hello user! Which operation would you like to use?
  2. 1) +
  3. 2) -
  4. 3) *
  5. 4) /
  6. a
  7. Enter an integer only.
  8. 5
  9. Invalid input
  10. 2
  11. subtractingMethod()

答案2

得分: 0

你还可以处理在default中的使用情况。完全取决于您的用例,您可以如下处理异常,还可以创建自定义异常并从default中抛出,类似于:

  1. System.out.println("Hello user! Which operation would you like to use?");
  2. System.out.println("1) 加法\n2) 减法\n3) 乘法\n4) 除法");
  3. Scanner operacijai = new Scanner(System.in);
  4. int operacija = operacijai.nextInt();
  5. int n=1;
  6. do {
  7. try {
  8. switch (operacija) {
  9. case 1:
  10. addingMethod();
  11. n=2;
  12. break;
  13. case 2:
  14. subtractingMethod();
  15. n=2;
  16. break;
  17. case 3:
  18. multiplyingMethod();
  19. n=2;
  20. break;
  21. case 4:
  22. dividingMethod();
  23. n=2;
  24. break;
  25. default:
  26. System.out.print("输入正确的数字!");
  27. throw new CustomException();
  28. }
  29. }
  30. catch(CustomException e) {
  31. System.out.print("输入正确的数字!");
  32. }
  33. } while(n==1);
  34. operacijai.close();

注意:这里的代码只是翻译了您提供的部分内容,并没有对代码逻辑进行任何更改。

英文:

You can also handle the use case in default
It is totally upto your use-case how you are handling the exception, you can also create your custom exception and throw from default
something like:

  1. System.out.println(&quot;Hello user! Which operation would you like to use?&quot;);
  2. System.out.println(&quot;1) + \n2) - \n3) * \n4) /&quot;);
  3. Scanner operacijai = new Scanner(System.in);
  4. int operacija = operacijai.nextInt();
  5. int n=1;
  6. do {
  7. try {
  8. switch (operacija) {
  9. case 1:
  10. addingMethod();
  11. n=2;
  12. break;
  13. case 2:
  14. subtractingMethod();
  15. n=2;
  16. break;
  17. case 3:
  18. multiplyingMethod();
  19. n=2;
  20. break;
  21. case 4:
  22. dividingMethod();
  23. n=2;
  24. break;
  25. default:
  26. System.out.print(&quot;Enter a correct number!&quot;)
  27. throw new CustomException();
  28. }
  29. }
  30. catch(CustomException e) {
  31. System.out.print(&quot;Enter a correct number!&quot;);
  32. }
  33. } while(n==1);
  34. operacijai.close();
  35. }

答案3

得分: 0

已找到一种使用默认情况的清晰方法。

  1. public static void main(String[] args) {
  2. System.out.println("你好!请选择要使用的操作:");
  3. System.out.println("1) 加法\n2) 减法\n3) 乘法\n4) 除法");
  4. Scanner operacijai = new Scanner(System.in);
  5. int operacija;
  6. do {
  7. operacija = operacijai.nextInt();
  8. switch (operacija) {
  9. case 1:
  10. addingMethod();
  11. break;
  12. case 2:
  13. subtractingMethod();
  14. break;
  15. case 3:
  16. multiplyingMethod();
  17. break;
  18. case 4:
  19. dividingMethod();
  20. break;
  21. default:
  22. System.out.print("请输入正确的数字!");
  23. }
  24. } while (operacija < 1 || operacija > 4);
  25. operacijai.close();
  26. }
英文:

Figured out a clean way of doing this with default case.

  1. System.out.println(&quot;Hello user! Which operation would you like to use?&quot;);
  2. System.out.println(&quot;1) + \n2) - \n3) * \n4) /&quot;);
  3. Scanner operacijai = new Scanner(System.in);
  4. int operacija;
  5. do {
  6. operacija = operacijai.nextInt();
  7. switch (operacija) {
  8. case 1:
  9. addingMethod();
  10. break;
  11. case 2:
  12. subtractingMethod();
  13. break;
  14. case 3:
  15. multiplyingMethod();
  16. break;
  17. case 4:
  18. dividingMethod();
  19. break;
  20. default:
  21. System.out.print(&quot;Enter a correct number!&quot;);
  22. }
  23. } while(operacija &lt; 1 || operacija &gt; 4);
  24. operacijai.close();
  25. }
  26. </details>

huangapple
  • 本文由 发表于 2020年4月6日 02:54:49
  • 转载请务必保留本文链接:https://java.coder-hub.com/61047815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定