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

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

How to remove the amounts of ifs from the application?

问题

以下是简化后的代码:

  1. private static final Scanner scanner = new Scanner(System.in);
  2. public static void main(String[] args) {
  3. int N = scanner.nextInt();
  4. scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  5. scanner.close();
  6. if (N % 2 == 1 || (N >= 6 && N <= 20)) {
  7. System.out.println("Weird");
  8. } else if (N >= 2 && N <= 5) {
  9. System.out.println("Not Weird");
  10. } else if (N > 20) {
  11. System.out.println("Not Weird");
  12. }
  13. }

这个简化后的代码只使用了两个条件语句,分别处理"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:

  1. private static final Scanner scanner = new Scanner(System.in);
  2. public static void main(String[] args) {
  3. int N = scanner.nextInt();
  4. scanner.skip(&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;);
  5. scanner.close();
  6. int Numberparorimpa = N % 2;
  7. if(N &lt; 2 || Numberparorimpa ==1 || N &lt;=20 &amp;&amp; N &gt;=6 ){
  8. System.out.println(&quot;Weird&quot;);
  9. }else{
  10. if(N &gt;=2 &amp;&amp; Numberparorimpa == 0){
  11. System.out.println(&quot;Not Weird&quot;);
  12. }else{
  13. if(Numberparorimpa == 0 &amp;&amp; N &gt;=6 || N&lt;=20){
  14. System.out.println(&quot;Weird&quot;);
  15. }else{
  16. if(Numberparorimpa== 0 &amp;&amp; N&gt; 20){
  17. System.out.println(&quot;Not Weird&quot;);
  18. }else{
  19. return;
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }

How can I reduce the IFs of this code?

答案1

得分: 1

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

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

英文:

I think an optimize version could be this :

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

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

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

示例:

  1. public static void main(String[] args) {
  2. int n = scanner.nextInt();
  3. scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  4. scanner.close();
  5. String print = isGivenNumberWeird(n) ? "Weird" : "Not Weird";
  6. System.out.println(print);
  7. }
  8. private static boolean isGivenNumberWeird(int n) {
  9. boolean isOdd = n % 2 == 1;
  10. if (isOdd) {
  11. return true;
  12. }
  13. if (n >= 2 && n <= 5) {
  14. return false;
  15. }
  16. if (n >= 6 && n <= 20) {
  17. return true;
  18. }
  19. if (n > 20) {
  20. return false;
  21. }
  22. }
英文:

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:

  1. public static void main(String[] args) {
  2. int n = scanner.nextInt();
  3. scanner.skip(&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;);
  4. scanner.close();
  5. String print = isGivenNumberWeird(n) ? &quot;Weird&quot; : &quot;Not Weird&quot;
  6. System.out.println(print);
  7. }
  8. private static boolean isGivenNumberWeird(int n) {
  9. boolean isOdd = n % 2 == 1;
  10. if (isOdd) {
  11. return true;
  12. }
  13. if (n &gt;= 2 &amp;&amp; n &lt;=5) {
  14. return false;
  15. }
  16. if (n &gt;= 6 &amp;&amp; n &lt;=20) {
  17. return true;
  18. }
  19. if (n &gt; 20) {
  20. return false;
  21. }
  22. }

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:

确定