gpt4 book ai didi

java - 给定一个整数 N 作为输入,你能检查以下 :

转载 作者:行者123 更新时间:2023-11-30 06:47:33 25 4
gpt4 key购买 nike

给定一个整数 N 作为输入,你能检查以下内容吗:

如果 N 是奇数则打印“Weird”如果 N 是偶数,并且在范围 2 和 5(含)之间,则打印“Not Weird”如果 N 是偶数,并且在 6 到 20(含)之间,打印“Weird”如果 N 是偶数并且 ,打印“Not Weird”

我写了这段代码--

public class Solution {

public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
int ip;
Scanner sc = new Scanner(System.in);
ip = sc.nextInt();
if(ip%2==0 ){
if(ip>=6 || ip<=20){
System.out.println("Weird");
}
System.out.println("Not Weird");
}
else{
System.out.println("Weird");
}
}
}

失败了 24 -输出-诡异的不奇怪

预计不奇怪。

谁能告诉我错误?

最佳答案

print "Not Weird" If N is even and, in between range 6 and 20(inclusive)

这表明 N 应该在 6 到 20 之间并且你写了

if(ip>=6 || ip<=20)

这 if 考虑所有 ip 大于或等于 6 或所有 ip 小于等于 20,所以所有数字。

您应该使用&& 运算符而不是|| 运算符

您的代码应该是:

    ...
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN.
Print output to STDOUT. Your class should be named Solution. */
int ip;
Scanner sc = new Scanner(System.in);
ip = sc.nextInt();
if(ip%2==0 ){
if(ip>=6 && ip<=20){
System.out.println("Weird");
} else {
System.out.println("Not Weird");
}
} else {
System.out.println("Weird");
}
}
...

关于java - 给定一个整数 N 作为输入,你能检查以下 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45816427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com