Too many 'if' statements?

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

    0 讨论(0)
  • 2020-11-30 16:55

    As I draw a table between one/two and the result, I see one pattern,

    if(one<2 && two <2) result=0; return;
    

    The above would cut down atleast 3 if statements. I don't see a set pattern nor I am able to glean much from the code given - but if such logic can be derived, it would cut down a number of if statements.

    Hope this helps.

    0 讨论(0)
  • 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; }
      }
    }
    
    0 讨论(0)
  • 2020-11-30 16:59

    static int val(int i, int u){ int q = (i & 1) ^ (u & 1); return ((i >> 1) << (1 ^ q))|((u >> 1) << q); }

    0 讨论(0)
  • 2020-11-30 16:59

    Instead do something like this

       public int fightMath(int one, int two) {
        return Calculate(one,two)
    
        }
    
    
        private int Calculate(int one,int two){
    
        if (one==0){
            if(two==0){
         //return value}
        }else if (one==1){
       // return value as per condtiion
        }
    
        }
    
    0 讨论(0)
  • 2020-11-30 17:01

    I personally like to cascade ternary operators:

    int result = condition1
        ? result1
        : condition2
        ? result2
        : condition3
        ? result3
        : resultElse;
    

    But in your case, you can use:

    final int[] result = new int[/*16*/] {
        0, 0, 1, 2,
        0, 0, 2, 1,
        2, 1, 3, 3,
        1, 2, 3, 3
    };
    
    public int fightMath(int one, int two) {
        return result[one*4 + two];
    }
    

    Or, you can notice a pattern in bits:

    one   two   result
    
    section 1: higher bits are equals =>
    both result bits are equals to that higher bits
    
    00    00    00
    00    01    00
    01    00    00
    01    01    00
    10    10    11
    10    11    11
    11    10    11
    11    11    11
    
    section 2: higher bits are different =>
    lower result bit is inverse of lower bit of 'two'
    higher result bit is lower bit of 'two'
    
    00    10    01
    00    11    10
    01    10    10
    01    11    01
    10    00    10
    10    01    01
    11    00    01
    11    01    10
    

    So you can use magic:

    int fightMath(int one, int two) {
        int b1 = one & 2, b2 = two & 2;
        if (b1 == b2)
            return b1 | (b1 >> 1);
    
        b1 = two & 1;
    
        return (b1 << 1) | (~b1);
    }
    
    0 讨论(0)
提交回复
热议问题