Too many 'if' statements?

前端 未结 26 1091
天涯浪人
天涯浪人 2020-11-30 16:24

The following code does work how I need it to, but it\'s ugly, excessive or a number of other things. I\'ve looked at formulas and attempted to write a few solutions, but I

26条回答
  •  渐次进展
    2020-11-30 16:53

    Try it with switch casing...

    Take a look here or here for more info about it

    switch (expression)
    { 
      case constant:
            statements;
            break;
      [ case constant-2:
            statements;
            break;  ] ...
      [ default:
            statements;
            break;  ] ...
    }
    

    You can add multiple conditions(not simultaneously) to it and even have a default option where no other cases have been satisfied.

    PS: Only if one condition is to be satisfied..

    If 2 conditions arise simultaneously.. I don't think switch can be used. But you can reduce your code here.

    Java switch statement multiple cases

提交回复
热议问题