简体   繁体   中英

How can I simplify this Java code?

I am fairly new in Java and need to do some ugly if / else code.

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':

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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