java - 使用 switch case 的三个数字中最大的一个

标签 java bluej

我想使用 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;
    }
}

有人有更简单的答案吗?

最佳答案

这有点愚蠢,但你就可以了。

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

关于java - 使用 switch case 的三个数字中最大的一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11484132/

相关文章:

java - Java 中的不兼容类型。希望得到一些帮助

java - 检查密码

java - 下载并替换java中正在运行的可执行文件

java - 单击 RadioButton 即可扩展 PrimeFaces 中的折叠面板

java - 如何在运行时更改 AndroidManifest?

java - 如何从同一类的另一个对象调用变量

java - 如何在不使用 return 和 System.exit() 的情况下退出我的程序?

java - mule https 入站端点

java - Guice 初学者 - 如何绑定(bind)具体类 - 单例?

java - Java中的"int cannot be dereferenced"