【问题标题】:Greatest of three numbers using switch case [closed]使用开关盒的三个数字中的最大值[关闭]
【发布时间】:2012-07-14 02:38:01
【问题描述】:

我想找出三个给定数字中最大的数字,使用 switch-case(不使用 if) 我使用这个程序回答了这个问题,它有效:

class GreatestNoSwitch{
    public int main(int a, int b, int c){
        int d = (int)Math.floor(a/b);
        int max = 0;
        switch(d){
            case 0:
                max = b;
                break;
            default:
                max = a;
        }

        d = (int)Math.floor(max/c);

        switch(d){
            case 0:
                max = c;
        }
        return max;
    }
}

有人有更简单的答案吗?

【问题讨论】:

  • 您的问题是什么?你的代码有效吗?
  • StackOverflow 是 Question & Answering 站点。发布question,我们会尽力寻找答案或至少帮助您找到答案。
  • 如果不限制允许的功能,这个问题没有多大意义。

标签: java bluej


【解决方案1】:

不知道为什么要编写世界上最复杂的 sn-p 代码来查找三个整数的最大值。这个更易读,但仍然足够复杂,让您感到有趣......

public int main( int a, int b, int c)
{
    return Collections.max( Arrays.asList( new Integer[]{a,b,c} ));
}

【讨论】:

    【解决方案2】:

    这有点愚蠢,但给你。

    switch(1)
    {
        default:
            return Math.max(a, Math.max(b, c));
    }
    

    【讨论】:

    • @MarkoTopolnik - 更好?
    • 为什么会更好?您仍在使用 case 子句中的变量。
    • @MarkoTopolnik - 只有常量......我明白了。无论如何,这是一个愚蠢的难题,我的回答同样愚蠢。所以他们适合!
    • 确实完美!这正是我所说的如果没有对允许的函数的限制,这个谜题是愚蠢的。
    • 我现在觉得这是一个愚蠢的谜题......谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 2023-03-31
    • 2021-03-30
    • 1970-01-01
    相关资源
    最近更新 更多