Too many 'if' statements?

前端 未结 26 1183
天涯浪人
天涯浪人 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:56

    Thanks to @Joe Harper as I ended up using a variation of his answer. To slim it down further as 2 results per 4 were the same I slimmed it down further.

    I may come back to this at some point, but if there's no major resistance caused by multiple if-statements then I'll keep this for now. I will look into the table matrix and switch statement solutions further.

    public int fightMath(int one, int two) {
      if (one === 0) {
        if (two === 2) { return 1; }
        else if(two === 3) { return 2; }
        else { return 0; }
      } else if (one === 1) {
        if (two === 2) { return 2; }
        else if (two === 3) { return 1; }
        else { return 0; }
      } else if (one === 2) {
        if (two === 0) { return 2; }
        else if (two === 1) { return 1; }
        else { return 3; }
      } else if (one === 3) {
        if (two === 0) { return 1; }
        else if (two === 1) { return 2; }
        else { return 3; }
      }
    }
    

提交回复
热议问题