关于ifs的问题

Gur*_*epS 1 java

我有以下代码:

     boolean Short = x();
    boolean Long = y();       
    boolean Longer = z();

    if (Short )
        return "abc";

    if (Long)
        return "def";

    if (Longer) 
        return "ghi";        
Run Code Online (Sandbox Code Playgroud)

三个bool方法(x,y,z)都在数值上做数学.如果上面的3个ifs没有被评估为true,我需要一个额外的if语句来返回数字.怎么可能这样做,有没有多余的ifs?另外,我需要了解ifs的优先级?我的名字是"dotnet",但我同样是Java的程序员(我花了很多时间尝试将其提取到.NET).

谢谢

Ode*_*ded 5

没关系,但如果你不需要另外一个:

boolean Short = x();
boolean Long = y();       
boolean Longer = z();

if (Short )
    return "abc";

if (Long)
    return "def";

if (Longer) 
    return "ghi"; 

return "none of the above";
Run Code Online (Sandbox Code Playgroud)

if报告将评估才能,一旦其中一个是真,return声明将结束在方法中执行,所以没有什么会进行评估之后.

如果它们都不为真,则最后一个return将结束执行.

如果您不需要进行求值y(),z()何时x()为真,z()则可以用变量本身替换变量,也不y()是为真:

if (x())
    return "abc";

if (y())
    return "def";

if (z()) 
    return "ghi"; 

return "none of the above";
Run Code Online (Sandbox Code Playgroud)