使用if else语句来扫描并打印奇数和偶数数字。

huangapple go评论66阅读模式
英文:

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(&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;);

        scanner.close();

        String ans = &quot;&quot;;

        if ((N % 2) == 1) {
            ans += &quot;Weird&quot;;
            System.out.println(ans);
        } 
        else if ((N % 2) == 0) {
            if (N &gt;= 2 || N &lt;= 5) {
                ans += &quot;Not Weird&quot;;
                System.out.println(ans);
            }
        } 
        else if ((N % 2) == 0) {
            if (N &gt;= 6 || N &lt;= 20) {
                ans += &quot;Weird&quot;;
                System.out.println(ans);
            }
        } 
        else if ((N % 2) == 0) {
            if (N &gt; 20) {
                ans += &quot;Not Weird&quot;;
                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(&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;);

        scanner.close();

        String ans = &quot;&quot;;

        if ((N % 2) == 1) {
            ans += &quot;Weird&quot;;
        } else {
            if (N &lt;= 5) {
                ans += &quot;Not Weird&quot;;
            } else if (N &lt;= 20) {
                ans += &quot;Weird&quot;;
            } else {
                ans += &quot;Not Weird&quot;;
            }
        }
        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 ? &quot;Weird&quot;
				: (n &gt;= 2 &amp;&amp; n &lt;= 5 ? &quot;Not Weird&quot; : (n &gt;= 6 &amp;&amp; n &lt;= 20 ? &quot;Weird&quot; : (n &gt; 20 ? &quot;Not Weird&quot; : &quot;&quot;)))

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&lt;Integer&gt; testNumbers = IntStream.rangeClosed(1, 25).boxed().collect(Collectors.toList());
		for (int n : testNumbers) {
			System.out.println(n + &quot; -&gt; &quot; + whatIsIt(n));
		}
	}

	static String whatIsIt(int n) {
		return n % 2 != 0 ? &quot;Weird&quot;
				: (n &gt;= 2 &amp;&amp; n &lt;= 5 ? &quot;Not Weird&quot; : (n &gt;= 6 &amp;&amp; n &lt;= 20 ? &quot;Weird&quot; : (n &gt; 20 ? &quot;Not Weird&quot; : &quot;&quot;)));
	}
}

Output:

1 -&gt; Weird
2 -&gt; Not Weird
3 -&gt; Weird
4 -&gt; Not Weird
5 -&gt; Weird
6 -&gt; Weird
7 -&gt; Weird
8 -&gt; Weird
9 -&gt; Weird
10 -&gt; Weird
11 -&gt; Weird
12 -&gt; Weird
13 -&gt; Weird
14 -&gt; Weird
15 -&gt; Weird
16 -&gt; Weird
17 -&gt; Weird
18 -&gt; Weird
19 -&gt; Weird
20 -&gt; Weird
21 -&gt; Weird
22 -&gt; Not Weird
23 -&gt; Weird
24 -&gt; Not Weird
25 -&gt; 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 += &quot;Weird&quot;;
}
else { //We know N is not odd, so it must be even
    if (N &lt;= 5) {
        ans += &quot;Not Weird&quot;;
    }
    else if (N &lt;= 20) { //We know N&lt;=5 is false, so N&gt;5 must be true
        ans += &quot;Weird&quot;;
    }
    else{ //we know N&lt;=20 is false, so N &gt;20 must be true
        ans += &quot;Not Weird&quot;;
    }
}
System.out.println(ans);

huangapple
  • 本文由 发表于 2020年4月8日 19:52:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/61100016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定