如何从应用程序中删除大量的条件语句?

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

How to remove the amounts of ifs from the application?

问题

以下是简化后的代码:

private 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();
    
    if (N % 2 == 1 || (N >= 6 && N <= 20)) {
        System.out.println("Weird");
    } else if (N >= 2 && N <= 5) {
        System.out.println("Not Weird");
    } else if (N > 20) {
        System.out.println("Not Weird");
    }
}

这个简化后的代码只使用了两个条件语句,分别处理"Weird"和"Not Weird"的情况,消除了多余的嵌套条件。

英文:

The code I say has to complete these assignments:
Given an integer,N, perform the following conditional actions:

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
Complete the stub code provided in your editor to print whether or not N is weird.

My code looked like this:

private 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();
        int Numberparorimpa = N % 2;
        if(N &lt; 2 || Numberparorimpa ==1 || N &lt;=20 &amp;&amp;  N &gt;=6 ){
            System.out.println(&quot;Weird&quot;);
        }else{
            if(N &gt;=2 &amp;&amp; Numberparorimpa == 0){
                 System.out.println(&quot;Not Weird&quot;);
            }else{
             if(Numberparorimpa == 0 &amp;&amp; N &gt;=6 || N&lt;=20){
                  System.out.println(&quot;Weird&quot;);
             }else{
                 if(Numberparorimpa== 0 &amp;&amp; N&gt; 20){
                     System.out.println(&quot;Not Weird&quot;);
                 }else{
                     return;
                 }
             }   
            }
        }
            }
        
    }

How can I reduce the IFs of this code?

答案1

得分: 1

如果N是奇数或在6到20的范围内,它就是奇怪的。

否则,N要么是偶数,要么不在范围内,所以它不奇怪。

英文:

I think an optimize version could be this :

if (N % 2 == 1 || (N &gt;= 6 &amp;&amp; N &lt;= 20)) {
  System.out.println(&quot;Weird&quot;);
}
else {
  System.out.println(&quot;Not Weird&quot;);
}

If N is odd or N in range of 6 to 20 it's weird.

Else N is either even or not in the range so it's not weird.

答案2

得分: 1

你可以通过将逻辑提取到方法/类等方式来减少代码的复杂性并提高可读性。此外,嵌套条件很难阅读,应该避免使用。

示例:

public static void main(String[] args) {

    int n = scanner.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    scanner.close();

    String print = isGivenNumberWeird(n) ? "Weird" : "Not Weird";
    System.out.println(print);
}

private static boolean isGivenNumberWeird(int n) {
    boolean isOdd = n % 2 == 1;
    if (isOdd) {
        return true;
    }
    if (n >= 2 && n <= 5) {
        return false;
    }
    if (n >= 6 && n <= 20) {
        return true;
    }
    if (n > 20) {
        return false;
    }
}
英文:

You can reduce the complexity and improve the readability of your code by extracting your logic to method/class etc. Also, nested conditions are hard to read, you should avoid it.

Example:

public static void main(String[] args) {

	int n = scanner.nextInt();
    scanner.skip(&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;);
    scanner.close();

    String print = isGivenNumberWeird(n) ? &quot;Weird&quot; : &quot;Not Weird&quot;
	System.out.println(print);
}

private static boolean isGivenNumberWeird(int n) {
	boolean isOdd = n % 2 == 1;
    if (isOdd) {
		return true;
    }
    if (n &gt;= 2 &amp;&amp; n &lt;=5) {
    	return false;
    }
    if (n &gt;= 6 &amp;&amp; n &lt;=20) {
    	return true;
    }
    if (n &gt; 20) {
    	return false;
    }
}

huangapple
  • 本文由 发表于 2023年1月9日 16:32:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054751.html
匿名

发表评论

匿名网友

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

确定