【问题标题】:How to do nested if - else with 4 numbers to find biggest number如何嵌套 if - else 用 4 个数字找到最大的数字
【发布时间】:2020-05-11 16:05:30
【问题描述】:
public class NastingIfElse {

    public static void main(String[] args) {

        int a = 998 , b = 857 , c = 241 , d = 153;
        int result;

        if ( a > b ) {
            if ( a > c ) {
                if ( a > d ) {
                    result = a;
                }else {
                    result = d;
                }
            }
        }
        if ( b > c ) {
            if ( b > d ) {
                result = b;
            }else {
                result = d;
            }
        }
        if ( c > a ) {
            if ( c > d ) {
                result = c;
            }else {
                result = d;
            }
        }
        if ( d > a ) {
            if ( d > c ) {
                if ( d > b ) {
                    result = d;
                }else {
                    result = b;
                }
            }
        }



        System.out.println("Biggest number of three is " + result);


    }

}

我想做这个代码并从这 4 个数字中找出最大的数字。 但是这个“嵌套if程序”有一些问题。 所以我需要找到一些方法来运行这个特定的程序,以便仅使用“嵌套 if”从这些数字中找到最高数字。

所以在这个程序中帮助我。

【问题讨论】:

  • 您可以将所有内容替换为:Math.max(Math.max(Math.max(a,b),c), d); 如果您有任意数字列表,则可以在循环中执行此操作,Math.max(max_of_last_pair, current_number);
  • "但是有一些问题" => 那你为什么不告诉我们你有什么问题呢?
  • 顺便说一下,你的输出应该是"Biggest number of four is "。 :)
  • 是不是“必须”通过嵌套的 if 和 else 来完成的课程练习?
  • 是的,Majid Roustaei 上了一课。

标签: java class if-statement numbers nested-if


【解决方案1】:

如果是某种必须嵌套 if 和 else 的练习,试试这个:

    int a = 998 , b = 857 , c = 241 , d = 153;
    int result;

    if ( a > b ) {
        if ( a > c ) {
            if ( a > d ) {
                result = a;
            }
            else {
                result = d;
            }
        }
        else if ( c > d ) {
            result = c;
        }
        else {
            result = d;
        }
    }
    else if ( b > c ) {
        if ( b > d ) {
            result = b;
        }
        else {
            result = d;
        }
    }
    else if ( c > d ) {
        result = c;
    }
    else {
        result = d;
    }

否则在 java 中确实有更好的选择,支持更多的数字。

【讨论】:

    【解决方案2】:
    int a = 545, b = 24, c = 75454, d = 68;
    int result = 0;
    if (a > b) {
        if (a > c && a > d) {
            result = a;
        } else {
            if (c > d) {
                result = c;
            } else {
                result = d;
            }
        }
    } else {
        if (b > c && b > d) {
            result = b;
        } else {
            if (c > d) {
                result = c;
            } else {
                result = d;
            }
        }
    }
    
    System.out.println(result);
    

    我编写的代码超级短且易于实现

    【讨论】:

    • 我错过了你第一个问题的第二部分。基本上,你的答案越好(内容好,格式好,易于阅读),就越有可能吸引赞成票。反之亦然。如果您还没有,请查看How to Answer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2023-02-04
    • 1970-01-01
    • 2022-08-14
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多