10

这是一些示例代码行..

if(loc > 0 || cat > 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built) {    
 /// Condition to checn all true
    return true;
} else if(loc < 0 || cat > 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built) { 
   /////// 1 false other are true 

} else if(loc > 0 || cat < 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built) { 

}

如何处理这些情况。如果我有 5 个陈述。那么它必须一个一个地接近 12+ 个条件。如果我要检查所有 5 种组合,它会使用更多的代码行,我们是否有更好的选择来检查所有条件。

4

5 回答 5

12

如果您在 javascript 中将布尔表达式视为整数,它将计算为0(for false) 或1(for true)。因此,您可以对条件求和,然后使用 switch-case 构造来检查有多少是真的:

var numTrue = 
   (loc > 0) + (cat > 0) + (price > 0) + (jsBed <= bedroom) + (jsBuilt >= built);

switch(numTrue) {
    case 0:
        // do something if no condition is met
        break;
    case 1:
        // do something else if one condition is met
        break;
    // etc...
}
于 2017-05-31T07:05:35.050 回答
4

你有永远不会满足的条件:

if(loc > 0 || cat > 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built){    
    /// Condition to checn all true
    return true;
} else if(loc < 0 || cat > 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built) { 
/////// 1 false other are true 

} else if(loc > 0 || cat < 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built) { 

}

基本上 :

  • 在第二个 else if 中,条件 cat > 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built是无用的,因为第一个条件已经满足。由于您使用 an,else if他们将已经输入第一个 if。所以唯一重要的是loc < 0
  • elseif仅与最后相同cat < 0是相关的。

所以可以重写为

if(loc > 0 || cat > 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built){    
    /// Condition to checn all true
    return true;
} else if(loc < 0) { 
/////// 1 false other are true 

} else if(cat < 0) { 

}

此答案假定提供的代码是您要简化的代码,而不是通用示例。

注意:我认为您可能没有写出您想要做的事情,忘记了一些 AND 而不是 OR。

于 2017-05-31T10:57:13.887 回答
3

5个条件是2**5,即32个组合。

如果您想检查各种组合,而无需重复测试,您可以位移各个结果并将它们组合成一个 switch 语句。直接处理数字很简洁,但可读性不强

var loc=1,cat=0,price=0,jsBed=1,bedroom=0,jsbuilt=0,built=1;

let results=[loc > 0,cat > 0,price > 0,jsBed <= bedroom,jsbuilt >= built];
let bits=results.reduce( (accum,current,index)=>accum+(current<<index), 0);
switch(bits){
case 0: // none
 break;
case 3: // first two
 break;
case 4: // third one
 break;
}

修改它以使用常量将使 switch 语句更具可读性

var loc=0,cat=1,price=0,jsBed=1,bedroom=0,jsbuilt=0,built=1;

const locBit=1<<0;
const catBit=1<<1;
const priceBit=1<<2;
const bedBit=1<<3;
const builtBit=1<<4;
let bits=( loc > 0 )*locBit |
         ( cat > 0 )*catBit |
         ( price > 0 )*priceBit |
         ( jsBed <= bedroom )*bedBit |
         ( jsbuilt >= built )*builtBit;
switch(bits){
  case 0:
      console.log("!loc,!cat,!price,!bed,!built");
      break;
  case catBit|locBit:
      console.log("loc,cat,!price,!bed>!built");
      break;
  default:
      console.log(bits);
}

你可以使用常量来帮助

于 2017-05-31T09:48:42.443 回答
1

edit1:为javascript修改,而不是java。哎呀...

我不确定您是否想查看所有组合,但您可以通过为每个可能的输出引入一个数值来对它们进行分组。

具体有5个变量和每个变量2个选项?我已经用二进制表示的数字设置了一个表。如果每个(或某些)变量有 > 2 个选项,则必须使用数字(以 10 为底)。您可以使用二进制值,例如

const locVal  = (loc > 0 ? 0x1 : 0x0) << 0;
const catVal  = (cat < 0 ? 0x1 : 0x0) << 1;
const priceVal= (price < 0 ? 0x1 : 0x0) << 2;
ect

因此,您可以将它们分组到一个方法中:

function foo(trueCond, level) {
    return (trueCond ? 0b1 : 0b0) << level;
}

这使得

const locVal  = foo(loc > 0, 0);
const catVal  = foo(cat > 0, 1);
const priceVal= foo(price > 0, 2)

(我省略了其他变量...)然后将二进制值相加

const total = locVal + catVal + priceVal

然后你现在必须使用 switch case 语句,如

switch (total) {
    case 0: // all options negative
    case 1: // only loc is positive
    case 2: // only cat is positive
    case 3: // both loc and cat is positive
    ect
}

中的值case表示存在于 中的二进制序列的整数值total。必须注意的是,将代码记录得非常好,尤其是案例块非常重要,这样其他读者就可以直接找出哪个值代表什么(就像我所做的那样)。

如果每个变量有两个以上的选项,您可以以 10 的因子工作(如在方法 foo 中,使用(trueCond ? 1 : 0) * Math.pow(10, level)

于 2017-05-31T07:37:22.200 回答
0

由于您的 3 个条件是固定的,因此您可以先拥有它们,然后再拥有其他条件,这可以转换为 switch-case。

if(price > 0 || jsBed <= bedroom || jsBuilt >= built) {
    var locCheck = (loc > 0) ? 1 : 0;
    var catCheck = (cat > 0) ? 1 : 0;
    switch(locCheck + catCheck){
        case 0:
            break;
        case 1:
            break;
        case 2:
            break;
        default:
            break;
    }
}
于 2017-05-31T07:08:53.030 回答