写一个方法,以3个整数为参数,相等则返回true,否则返回false,不使用if语句[关闭]

问题描述 投票:0回答:1

我正在尝试编写一个不使用 if 语句来比较三个整数的方法。我尝试了 switch 语句,但它不起作用。 Eclipse 显示 case 表达式必须是常量。

这是我的代码片段:

public boolean multipleValue(int n1, int n2, int n3)
{
    boolean result = false;

    switch (n1)
    {
    case num2:
    case num3:
        result = true;
        break;
    default:
        result = false;
    }
    
    return result;
}
```
java methods switch-statement
1个回答
2
投票

该行检查 a 是否等于 b 以及 b 是否等于 c。如果两个条件都为 true,则返回 true,否则返回 false。

return (a == b) && (b == c); 
© www.soinside.com 2019 - 2024. All rights reserved.