简体   繁体   English

如何简化此Java代码?

[英]How can I simplify this Java code?

I am fairly new in Java and need to do some ugly if / else code. 我是Java的新手,如果/ else代码需要做一些难看的事情。

if (st1 == 0 || st2 == 0 || st3 == 0) {
  if (st1 == 0) {
    return a;
  } else if (st2 == 0) {
    return b;
  } else {
    return c;
  }
}

But to me it seems like there should be some simpler way to do this kind of code. 但是对我来说,似乎应该有一些更简单的方法来执行这种代码。 (I know i could leave the outer if away, but it is for the purpose of showing the problem) (我知道如果离开我可以离开外面,但这只是为了显示问题)

I hope somebody has an idea on how to beautify this code :) 我希望有人对如何美化此代码有想法:)

Remove the outer condition, and remove unnecessary 'else': 删除外部条件,并删除不必要的“ else”:

if (st1 == 0) {
    return a;
}
if (st2 == 0) {
    return b;
}
if (st3 == 0) {
    return c;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM