提问人:Kavyanjali 提问时间:7/8/2023 最后编辑:Kavyanjali 更新时间:7/9/2023 访问量:48
我不知道我在给定的程序中所做的语法错误:[关闭]
I don't know the syntax error I am doing in the program given : [closed]
问:
任务 给定一个整数 ,执行以下条件操作:
如果 N 为奇数,则打印 Weird 如果 N 是偶数且在 to 的包含范围内,则打印 Not Weird 如果 N 是偶数且在 to 的包含范围内,则打印 Weird 如果 N 是偶数且大于 ,则打印 Not Weird 完成编辑器中提供的存根代码以打印是否奇怪。
程序:
import java.util.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
if(N = odd){
System.out.println("Weird");
} else if(N = even ; 2 < N < 5) {
System.out.println("Not Weird");
} else if(N = even ; 6<N<20 ) {
System.out.println("Weird");
} else{
System.out.println("Not Weird");
}
}
}
答:
-1赞
PSchickling
7/8/2023
#1
您可以使用模来检查数字是奇数还是偶数。然后对 if 条件使用逻辑 AND。%
&&
if(N%2 == 1){
System.out.println("Weird");
} else if(2 < N && N < 5) {
System.out.println("Not Weird");
} else if(6 < N && N < 20) {
System.out.println("Weird");
} else{
System.out.println("Not Weird");
}
评论
odd
N
N = odd
==