Java 如何以新的方式避免switch语句中默认条件下的错误
如何避免“default”中的switch语句出错?此switch case是根据新标准编写的Java 如何以新的方式避免switch语句中默认条件下的错误,java,switch-statement,Java,Switch Statement,如何避免“default”中的switch语句出错?此switch case是根据新标准编写的 import java.util.Scanner; public class SwitchCase { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Choose option: ");
import java.util.Scanner;
public class SwitchCase {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Choose option: ");
char userChoice = scan.next().charAt(0);
switch (userChoice) {
case '1' -> System.out.println("1 funkcja");
case '2' -> System.out.println("2 funkcja");
default ->
if((!Character.isDigit(userChoice))||(userChoice>3)){ #this part throws an error below:
System.out.println("Input error");
}
}
scan.close();
}
}
错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error, insert "ThrowExpression ;" to complete SwitchLabeledThrowStatement
Syntax error, insert "}" to complete SwitchBlock
Syntax error on token "}", delete this token
at SwitchCase.main(SwitchCase.java:11)
选中此项:
您需要使用:用于开关箱。
我们正在比较userChoice,它是char和3(一个int)
default->{/*if语句*/}
谢谢你的帮助
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Choose option: ");
char userChoice = scan.next().charAt(0);
switch (userChoice) {
case '1': System.out.println("1 funkcja");
case '2': System.out.println("2 funkcja");
default:
if(!Character.isDigit(userChoice)||(Integer.parseInt(String.valueOf(userChoice))>3)){ //#this part throws an error below:
System.out.println(userChoice + " Input error");
}
}
scan.close();
}