这是我的if-else声明:
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int number;
System.out.println("Enter Integer: ");
number = input.nextInt();
if (number % 7 == 0) {
System.out.println(number + " is divisible by 7");
}
else {
System.out.println(number + " isn't divisible by 7");
}
}
答案 0 :(得分:6)
别介意开关 - 您也不需要。
您可以将其转换为一个语句:
System.out.println(number + " is" + (number % 7 == 0 ? "" : "n't") + " divisible by 7");
答案 1 :(得分:3)
number = input.nextInt();
switch(number % 7)
{
case 0:
System.out.println(number + " is divisible by 7");
break;
default:
System.out.println(number + " isn't divisible by 7");
}
答案 2 :(得分:0)
number = input.nextInt();
a = number % 7
switch (a) {
case 0: System.out.println(number + " is divisible by 7");
break;
default: System.out.println(number + " isn't divisible by 7");
break;
}
答案 3 :(得分:0)
检查出来:
switch (number % 7) {
case 0:
System.out.println(number + " is divisible by 7");
break;
default:
System.out.println(number + " isn't divisible by 7");
}
OR
switch (number % 7) {
case 0:
System.out.println(number + " is divisible by 7");
break;
case 1: // Fall through
case 2:
case 3:
case 4:
case 5:
case 6:
System.out.println(number + " isn't divisible by 7");
}
答案 4 :(得分:0)
试试这样;
Scanner input = new Scanner (System.in);
int number;
System.out.println("Enter Integer: ");
number = input.nextInt();
int tempNumber = number % 7;
switch(tempNumber) {
case 0:
System.out.println(number + " is divisible by 7");
break;
default :
System.out.println(number + " isn't divisible by 7");
}