beatcraft
beatcraft

Reputation: 299

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

Upvotes: 0

Views: 93

Answers (1)

NorthernSky
NorthernSky

Reputation: 498

Remove the outer condition, and remove unnecessary 'else':

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

Upvotes: 4

Related Questions