从 if 语句算法中找出 returns 为真的值

Find out value that returns true from if statement -algorithm

另一个class将把随机数传入这个method(x,y,z)。我想知道我上一个 if() 语句中 returns 为真的 boolean,这样我就可以对其进行操作。我已经在评论中解释了我的逻辑。

我对这个还是很陌生,所以我的逻辑可能是错误的。

public static String FindDate(int x, int y, int z) {
    boolean istrue1 =(x >= 1 && x <= 31);
    boolean istrue2 =(y >= 1 && y <= 31);
    boolean istrue3 =(z >= 1 && z <= 31);
    if(istrue1 ^ istrue2){
        if(istrue1^istrue3){
            if(istrue2^istrue3){//now knowing that no values are the same, i can find the true value.
                if(istrue1||istrue2||istrue3){
                // I want to store/use/print/know which bool(istrue) that evaluated to true, so I would know if it is
                //x,y,z that went through the algorithm successfully.

                }
              }  else{return "Ambiguous";}


            }else{return "Ambiguous";}


        }else{return "Ambiguous";}
    return "true"; //I would actually end up returning the value that went through the algorithm
    }

你可以如下: 但是你的条件不正确 3 变量的两个值 (true, false) 都不能不同。所以你的逻辑总是return"Ambiguous"

 if(istrue1 ^ istrue2){
    if(istrue1^istrue3){
        if(istrue2^istrue3){
           //now knowing that no values are the same, i can find the true value.
           if(istrue1||istrue2||istrue3){
             // I want to store/use/print/know the bool that evaluated to true, so I would know if it is
            //x,y,z that went through the algorithm successfully.
            return "true";
      } 
 }     
return "Ambiguous";

您可以像在 Java 中存储任何其他类型一样存储布尔值。我不确定你最后的评论 "the value that went through" 到底是什么意思,但如果你想跟踪特定测试的结果,你不一定需要写类似

的东西
if (a == b) {
    return true;
} else {
    return false;
}

那样的话,

return a == b;

等价。如果表达式更复杂,parens 是个好主意。

return ((a == b) || c);

您可以随时存储结果并在以后对其进行处理,而不是返回。

bool test = ((a == b) || c);
action(test);

而不是:

if(istrue1||istrue2||istrue3)

将其分解为 3 个不同的 if 语句是最简单的。

if(istrue1)
if(istrue2)
if(istrue3)

没有我希望的简单技巧。不开心的一天。 此外,我对 xor(^) 运算符所做的陈述结果证明是错误的逻辑。

这样做最简单:

if(a&&b || a&&c || b&&c)

如果任何组合都为真,那将 return 模棱两可。 然而那不是最初的问题,但我想我不妨提一下。