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

huangapple 未分类评论54阅读模式
英文:

using if else statement to scan and print odd and even numbers

问题

  1. import java.util.*;
  2. public class Solution {
  3. public static final Scanner scanner = new Scanner(System.in);
  4. public static void main(String[] args) {
  5. int N = scanner.nextInt();
  6. scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  7. scanner.close();
  8. String ans = "";
  9. if ((N % 2) == 1) {
  10. ans += "Weird";
  11. System.out.println(ans);
  12. }
  13. else if ((N % 2) == 0) {
  14. if (N >= 2 && N <= 5) {
  15. ans += "Not Weird";
  16. System.out.println(ans);
  17. }
  18. else if (N >= 6 && N <= 20) {
  19. ans += "Weird";
  20. System.out.println(ans);
  21. }
  22. else if (N > 20) {
  23. ans += "Not Weird";
  24. System.out.println(ans);
  25. }
  26. }
  27. }
  28. }
英文:

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.

  1. import java.util.*;
  2. public class Solution {
  3. public static final Scanner scanner = new Scanner(System.in);
  4. public static void main(String[] args) {
  5. int N = scanner.nextInt();
  6. scanner.skip(&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;);
  7. scanner.close();
  8. String ans = &quot;&quot;;
  9. if ((N % 2) == 1) {
  10. ans += &quot;Weird&quot;;
  11. System.out.println(ans);
  12. }
  13. else if ((N % 2) == 0) {
  14. if (N &gt;= 2 || N &lt;= 5) {
  15. ans += &quot;Not Weird&quot;;
  16. System.out.println(ans);
  17. }
  18. }
  19. else if ((N % 2) == 0) {
  20. if (N &gt;= 6 || N &lt;= 20) {
  21. ans += &quot;Weird&quot;;
  22. System.out.println(ans);
  23. }
  24. }
  25. else if ((N % 2) == 0) {
  26. if (N &gt; 20) {
  27. ans += &quot;Not Weird&quot;;
  28. System.out.println(ans);
  29. }
  30. }
  31. }
  32. }

Edit: But when I input the number 18 instead of Weird the output comes as Not Weird., same for number 20.

答案1

得分: 2

尝试一下这段代码。

  1. import java.util.*;
  2. public class Solution {
  3. public static final Scanner scanner = new Scanner(System.in);
  4. public static void main(String[] args) {
  5. int N = scanner.nextInt();
  6. scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  7. scanner.close();
  8. String ans = "";
  9. if ((N % 2) == 1) {
  10. ans += "Weird";
  11. } else {
  12. if (N <= 5) {
  13. ans += "Not Weird";
  14. } else if (N <= 20) {
  15. ans += "Weird";
  16. } else {
  17. ans += "Not Weird";
  18. }
  19. }
  20. System.out.println(ans);
  21. }
  22. }
英文:

Try this code.

  1. import java.util.*;
  2. public class Solution {
  3. public static final Scanner scanner = new Scanner(System.in);
  4. public static void main(String[] args) {
  5. int N = scanner.nextInt();
  6. scanner.skip(&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;);
  7. scanner.close();
  8. String ans = &quot;&quot;;
  9. if ((N % 2) == 1) {
  10. ans += &quot;Weird&quot;;
  11. } else {
  12. if (N &lt;= 5) {
  13. ans += &quot;Not Weird&quot;;
  14. } else if (N &lt;= 20) {
  15. ans += &quot;Weird&quot;;
  16. } else {
  17. ans += &quot;Not Weird&quot;;
  18. }
  19. }
  20. System.out.println(ans);
  21. }
  22. }

答案2

得分: 1

使用三元运算符,您可以在一行代码中满足条件:

  1. n % 2 != 0 ? "Weird"
  2. : (n >= 2 && n <= 5 ? "Not Weird" : (n >= 6 && n <= 20 ? "Weird" : (n > 20 ? "Not Weird" : "")))

示例:

  1. import java.util.List;
  2. import java.util.stream.Collectors;
  3. import java.util.stream.IntStream;
  4. public class Main {
  5. public static void main(String[] args) {
  6. // 创建一个从1到25的测试整数列表
  7. List<Integer> testNumbers = IntStream.rangeClosed(1, 25).boxed().collect(Collectors.toList());
  8. for (int n : testNumbers) {
  9. System.out.println(n + " -> " + whatIsIt(n));
  10. }
  11. }
  12. static String whatIsIt(int n) {
  13. return n % 2 != 0 ? "Weird"
  14. : (n >= 2 && n <= 5 ? "Not Weird" : (n >= 6 && n <= 20 ? "Weird" : (n > 20 ? "Not Weird" : "")));
  15. }
  16. }

输出:

  1. 1 -> Weird
  2. 2 -> Not Weird
  3. 3 -> Weird
  4. 4 -> Not Weird
  5. 5 -> Weird
  6. 6 -> Weird
  7. 7 -> Weird
  8. 8 -> Weird
  9. 9 -> Weird
  10. 10 -> Weird
  11. 11 -> Weird
  12. 12 -> Weird
  13. 13 -> Weird
  14. 14 -> Weird
  15. 15 -> Weird
  16. 16 -> Weird
  17. 17 -> Weird
  18. 18 -> Weird
  19. 19 -> Weird
  20. 20 -> Weird
  21. 21 -> Weird
  22. 22 -> Not Weird
  23. 23 -> Weird
  24. 24 -> Not Weird
  25. 25 -> Weird
英文:

Using ternary operators, you can fulfil the criteria in just one line of code:

  1. n % 2 != 0 ? &quot;Weird&quot;
  2. : (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:

  1. import java.util.List;
  2. import java.util.stream.Collectors;
  3. import java.util.stream.IntStream;
  4. public class Main {
  5. public static void main(String[] args) {
  6. // Create a list of test integers from 1 to 25
  7. List&lt;Integer&gt; testNumbers = IntStream.rangeClosed(1, 25).boxed().collect(Collectors.toList());
  8. for (int n : testNumbers) {
  9. System.out.println(n + &quot; -&gt; &quot; + whatIsIt(n));
  10. }
  11. }
  12. static String whatIsIt(int n) {
  13. return n % 2 != 0 ? &quot;Weird&quot;
  14. : (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;)));
  15. }
  16. }

Output:

  1. 1 -&gt; Weird
  2. 2 -&gt; Not Weird
  3. 3 -&gt; Weird
  4. 4 -&gt; Not Weird
  5. 5 -&gt; Weird
  6. 6 -&gt; Weird
  7. 7 -&gt; Weird
  8. 8 -&gt; Weird
  9. 9 -&gt; Weird
  10. 10 -&gt; Weird
  11. 11 -&gt; Weird
  12. 12 -&gt; Weird
  13. 13 -&gt; Weird
  14. 14 -&gt; Weird
  15. 15 -&gt; Weird
  16. 16 -&gt; Weird
  17. 17 -&gt; Weird
  18. 18 -&gt; Weird
  19. 19 -&gt; Weird
  20. 20 -&gt; Weird
  21. 21 -&gt; Weird
  22. 22 -&gt; Not Weird
  23. 23 -&gt; Weird
  24. 24 -&gt; Not Weird
  25. 25 -&gt; Weird

答案3

得分: 0

  1. 如果在每种可能情况下都打印ans它可能位于if语句之外
  1. if ((N % 2) == 1) {
  2. ans += "奇怪";
  3. }
  4. else { //我们知道N不是奇数,所以它必须是偶数
  5. if (N <= 5) {
  6. ans += "不奇怪";
  7. }
  8. else if (N <= 20) { //我们知道N <= 5为假,所以N > 5必须为真
  9. ans += "奇怪";
  10. }
  11. else{ //我们知道N <= 20为假,所以N > 20必须为真
  12. ans += "不奇怪";
  13. }
  14. }
  15. System.out.println(ans);
英文:

Since you're printing ans in every possible case, it can be outside of the if statements.

  1. if ((N % 2) == 1) {
  2. ans += &quot;Weird&quot;;
  3. }
  4. else { //We know N is not odd, so it must be even
  5. if (N &lt;= 5) {
  6. ans += &quot;Not Weird&quot;;
  7. }
  8. else if (N &lt;= 20) { //We know N&lt;=5 is false, so N&gt;5 must be true
  9. ans += &quot;Weird&quot;;
  10. }
  11. else{ //we know N&lt;=20 is false, so N &gt;20 must be true
  12. ans += &quot;Not Weird&quot;;
  13. }
  14. }
  15. System.out.println(ans);

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

发表评论

匿名网友

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

确定