如何在Java中的switch语句中以新方式避免默认条件中的错误。

huangapple go评论94阅读模式
英文:

How to avoid error in default condition in the switch statement in the new way Java

问题

以下是已翻译的代码部分:

  1. 如何避免在default中的 switch 语句中出现错误 switch case 根据新标准编写
  2. import java.util.Scanner;
  3. public class SwitchCase {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6. System.out.print("选择选项:");
  7. char userChoice = scan.next().charAt(0);
  8. switch (userChoice) {
  9. case '1' -> System.out.println("1 函数");
  10. case '2' -> System.out.println("2 函数");
  11. default ->
  12. if ((!Character.isDigit(userChoice)) || (userChoice > 3)) { // 此部分会引发下面的错误:
  13. System.out.println("输入错误");
  14. }
  15. }
  16. scan.close();
  17. }
  18. }

一个错误:

  1. Exception in thread "main" java.lang.Error: 未解析的编译问题:
  2. 语法错误,插入“ThrowExpression ;”以完成 SwitchLabeledThrowStatement
  3. 语法错误,插入“}”以完成 SwitchBlock
  4. 在标记“}”上的语法错误,删除此标记
  5. at SwitchCase.main(SwitchCase.java:11)
英文:

How do I avoid an error in switch statement in "default"?This switch case is wiritten according to the new standrads.

  1. import java.util.Scanner;
  2. public class SwitchCase {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. System.out.print("Choose option: ");
  6. char userChoice = scan.next().charAt(0);
  7. switch (userChoice) {
  8. case '1' -> System.out.println("1 funkcja");
  9. case '2' -> System.out.println("2 funkcja");
  10. default ->
  11. if((!Character.isDigit(userChoice))||(userChoice>3)){ #this part throws an error below:
  12. System.out.println("Input error");
  13. }
  14. }
  15. scan.close();
  16. }
  17. }

An error:

  1. Exception in thread "main" java.lang.Error: Unresolved compilation problems:
  2. Syntax error, insert "ThrowExpression ;" to complete SwitchLabeledThrowStatement
  3. Syntax error, insert "}" to complete SwitchBlock
  4. Syntax error on token "}", delete this token
  5. at SwitchCase.main(SwitchCase.java:11)

答案1

得分: 1

这样检查:
你需要使用冒号(:)来分隔不同的switch case。
我们正在比较的是userChoice,它是一个字符,和3(一个整数)。

  1. public static void main(String[] args) {
  2. Scanner scan = new Scanner(System.in);
  3. System.out.print("选择选项:");
  4. char userChoice = scan.next().charAt(0);
  5. switch (userChoice) {
  6. case '1': System.out.println("1 函数");
  7. case '2': System.out.println("2 函数");
  8. default:
  9. if (!Character.isDigit(userChoice) || (Integer.parseInt(String.valueOf(userChoice)) > 3)) {
  10. System.out.println(userChoice + " 输入错误");
  11. }
  12. }
  13. scan.close();
  14. }
英文:

Check this:
You need to use : for switch cases.
We are comparing userChoice which is char to 3(an int).

  1. public static void main(String[] args) {
  2. Scanner scan = new Scanner(System.in);
  3. System.out.print("Choose option: ");
  4. char userChoice = scan.next().charAt(0);
  5. switch (userChoice) {
  6. case '1': System.out.println("1 funkcja");
  7. case '2': System.out.println("2 funkcja");
  8. default:
  9. if(!Character.isDigit(userChoice)||(Integer.parseInt(String.valueOf(userChoice))>3)){ //#this part throws an error below:
  10. System.out.println(userChoice + " Input error");
  11. }
  12. }
  13. scan.close();
  14. }

huangapple
  • 本文由 发表于 2020年10月7日 16:50:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64240513.html
匿名

发表评论

匿名网友

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

确定