Java 我在这一行出现了一个错误:案例1:boolean=(数字%7==0);
我在这行出现了一个错误:Java 我在这一行出现了一个错误:案例1:boolean=(数字%7==0);,java,Java,我在这行出现了一个错误: case 1: boolean = (number % 7 == 0); 这是我的全部代码: import java.util.Scanner; public class Question5 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number; System.out.println("Enter Inte
case 1: boolean = (number % 7 == 0);
这是我的全部代码:
import java.util.Scanner;
public class Question5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
System.out.println("Enter Integer: ");
number = input.nextInt();
String numberString;
switch (number) {
case 1: boolean = (number % 7 == 0);
System.out.println(number + " is divisible by 7");
break;
case 2: boolean = (number % 7 != 0);
System.out.println(number + " isn't divisible by 7");
break;
default:
}
}
}布尔是一种类型。它不能用作变量名。和变量必须在使用前声明
因此,这将是有效的代码,例如:
boolean divisibleBy7 = (number % 7 == 0)
if (divisibleBy7) {
System.out.println(number + " is divisible by 7");
}
else {
System.out.println(number + " isn't divisible by 7");
}
看来,你误用了switch语句。我明白了,您想要处理两种不同的情况,但不要使用switch语句。如果数字==1,则只输入案例1;如果数字==2,则输入案例2。对于所有其他数字,将输入默认大小写,其中不包含代码 我认为,您希望代码做的是这样的:
if (number % 7 == 0) {
System.out.println(number + " is divisible by 7");
} else {
System.out.println(number + " isn't divisible by 7");
}
你认为你要分配给哪个变量?撇开它不谈,它到底应该做什么?你打开了数字,这个问题的可能的副本也有关于把布尔语句放在案例旁边的内容。