英文:
using if else statement to scan and print odd and even numbers
问题
import java.util.*;
public class Solution {
public static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
scanner.close();
String ans = "";
if ((N % 2) == 1) {
ans += "Weird";
System.out.println(ans);
}
else if ((N % 2) == 0) {
if (N >= 2 && N <= 5) {
ans += "Not Weird";
System.out.println(ans);
}
else if (N >= 6 && N <= 20) {
ans += "Weird";
System.out.println(ans);
}
else if (N > 20) {
ans += "Not Weird";
System.out.println(ans);
}
}
}
}
英文:
I am having a problem with this if else statement. The requirement is:
- If n is odd, print Weird
- If n is even and in the inclusive range of 2 to 5, print Not Weird
- If n is even and in the inclusive range of 6 to 20, print Weird
- If n is even and greater than 20, print Not Weird.
When the input number is 18 the output is expected to be Weird. Same for the input number 20.
import java.util.*;
public class Solution {
public static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
scanner.close();
String ans = "";
if ((N % 2) == 1) {
ans += "Weird";
System.out.println(ans);
}
else if ((N % 2) == 0) {
if (N >= 2 || N <= 5) {
ans += "Not Weird";
System.out.println(ans);
}
}
else if ((N % 2) == 0) {
if (N >= 6 || N <= 20) {
ans += "Weird";
System.out.println(ans);
}
}
else if ((N % 2) == 0) {
if (N > 20) {
ans += "Not Weird";
System.out.println(ans);
}
}
}
}
Edit: But when I input the number 18 instead of Weird the output comes as Not Weird., same for number 20.
答案1
得分: 2
尝试一下这段代码。
import java.util.*;
public class Solution {
public static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
scanner.close();
String ans = "";
if ((N % 2) == 1) {
ans += "Weird";
} else {
if (N <= 5) {
ans += "Not Weird";
} else if (N <= 20) {
ans += "Weird";
} else {
ans += "Not Weird";
}
}
System.out.println(ans);
}
}
英文:
Try this code.
import java.util.*;
public class Solution {
public static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
scanner.close();
String ans = "";
if ((N % 2) == 1) {
ans += "Weird";
} else {
if (N <= 5) {
ans += "Not Weird";
} else if (N <= 20) {
ans += "Weird";
} else {
ans += "Not Weird";
}
}
System.out.println(ans);
}
}
答案2
得分: 1
使用三元运算符,您可以在一行代码中满足条件:
n % 2 != 0 ? "Weird"
: (n >= 2 && n <= 5 ? "Not Weird" : (n >= 6 && n <= 20 ? "Weird" : (n > 20 ? "Not Weird" : "")))
示例:
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
// 创建一个从1到25的测试整数列表
List<Integer> testNumbers = IntStream.rangeClosed(1, 25).boxed().collect(Collectors.toList());
for (int n : testNumbers) {
System.out.println(n + " -> " + whatIsIt(n));
}
}
static String whatIsIt(int n) {
return n % 2 != 0 ? "Weird"
: (n >= 2 && n <= 5 ? "Not Weird" : (n >= 6 && n <= 20 ? "Weird" : (n > 20 ? "Not Weird" : "")));
}
}
输出:
1 -> Weird
2 -> Not Weird
3 -> Weird
4 -> Not Weird
5 -> Weird
6 -> Weird
7 -> Weird
8 -> Weird
9 -> Weird
10 -> Weird
11 -> Weird
12 -> Weird
13 -> Weird
14 -> Weird
15 -> Weird
16 -> Weird
17 -> Weird
18 -> Weird
19 -> Weird
20 -> Weird
21 -> Weird
22 -> Not Weird
23 -> Weird
24 -> Not Weird
25 -> Weird
英文:
Using ternary operators, you can fulfil the criteria in just one line of code:
n % 2 != 0 ? "Weird"
: (n >= 2 && n <= 5 ? "Not Weird" : (n >= 6 && n <= 20 ? "Weird" : (n > 20 ? "Not Weird" : "")))
Demo:
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
// Create a list of test integers from 1 to 25
List<Integer> testNumbers = IntStream.rangeClosed(1, 25).boxed().collect(Collectors.toList());
for (int n : testNumbers) {
System.out.println(n + " -> " + whatIsIt(n));
}
}
static String whatIsIt(int n) {
return n % 2 != 0 ? "Weird"
: (n >= 2 && n <= 5 ? "Not Weird" : (n >= 6 && n <= 20 ? "Weird" : (n > 20 ? "Not Weird" : "")));
}
}
Output:
1 -> Weird
2 -> Not Weird
3 -> Weird
4 -> Not Weird
5 -> Weird
6 -> Weird
7 -> Weird
8 -> Weird
9 -> Weird
10 -> Weird
11 -> Weird
12 -> Weird
13 -> Weird
14 -> Weird
15 -> Weird
16 -> Weird
17 -> Weird
18 -> Weird
19 -> Weird
20 -> Weird
21 -> Weird
22 -> Not Weird
23 -> Weird
24 -> Not Weird
25 -> Weird
答案3
得分: 0
如果在每种可能情况下都打印ans,它可能位于if语句之外。
if ((N % 2) == 1) {
ans += "奇怪";
}
else { //我们知道N不是奇数,所以它必须是偶数
if (N <= 5) {
ans += "不奇怪";
}
else if (N <= 20) { //我们知道N <= 5为假,所以N > 5必须为真
ans += "奇怪";
}
else{ //我们知道N <= 20为假,所以N > 20必须为真
ans += "不奇怪";
}
}
System.out.println(ans);
英文:
Since you're printing ans in every possible case, it can be outside of the if statements.
if ((N % 2) == 1) {
ans += "Weird";
}
else { //We know N is not odd, so it must be even
if (N <= 5) {
ans += "Not Weird";
}
else if (N <= 20) { //We know N<=5 is false, so N>5 must be true
ans += "Weird";
}
else{ //we know N<=20 is false, so N >20 must be true
ans += "Not Weird";
}
}
System.out.println(ans);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论