If statement 错误的布尔方法?

If statement 错误的布尔方法?,if-statement,boolean,operators,If Statement,Boolean,Operators,我对布尔方法有一个问题,说明在注释中,但我不明白:为什么括号后面是“false”?我如何使用if语句来判断返回中的条件是正确的还是错误的?除了这些括号,我不能修改任何其他内容。我完全糊涂了。我理解下面的代码,首先我有条件(a和b)?“然后什么”但在第一段代码中我不明白。这是我的密码: public class Aufgabe5 { // returns true if (and only if) both x and y are in the range from 1 to 20 (i

我对布尔方法有一个问题,说明在注释中,但我不明白:为什么括号后面是“false”?我如何使用if语句来判断返回中的条件是正确的还是错误的?除了这些括号,我不能修改任何其他内容。我完全糊涂了。我理解下面的代码,首先我有条件(a和b)?“然后什么”但在第一段代码中我不明白。这是我的密码:

public class Aufgabe5 {

    // returns true if (and only if) both x and y are in the range from 1 to 20 (including 1 and 20)
    // and x is larger than y.
    private static boolean inRangeAndOrdered(int x, int y) {
        return (false /* TODO: modify this expression */);
    }

    // returns 1 if both a and b are true, 0 if a differs from b, and -1 if both a and b are false
    private static int wiredLogic(boolean a, boolean b) {
        return (a && b ? (-1 /* TODO: modify this expression */) : (-1 /* TODO: modify this expression */)) +
               (a || b ? (-1 /* TODO: modify this expression */) : (-1/* TODO: modify this expression */));
    }

我无法回答为什么
false
在括号后面,因为我不知道为什么你会在括号后面?这可能是你需要的一个奇怪的要求。听起来,出于某种原因,您需要
false
。但是,如果没有它,这种方法将更容易编写和理解

下面的方法将满足您在评论中的要求(同时使用
false

以下方法达到第二种方法评论中的要求:

// returns 1 if both a and b are true, 0 if a differs from b, and -1 if both a and b are false
private static int wiredLogic(boolean a, boolean b)
{
    return (a && b ? 1 : (a == b ? -1 : 0));
}
此返回语句执行三项检查以满足要求:

  • 如果a和b的组合为真,则返回1
  • 如果a和b的组合为非真且a等于b,则a和b都为假,返回-1
  • 如果a和b的组合为非真且a不等于b,则a为假且b为真或b为假且a为真,返回0

  • 我原以为这个网站也适合初学者,但正如我所看到的,如果这不是一个超级难的问题:让我们否决它!为了什么人?这个问题应该如何被投票表决?我受不了这些,如果你觉得容易的话,帮帮我。不要投反对票!
    if (false == !(x >= 1 && x <= 20 && y >= 1 && y <= 20))
        return true;
    else
        return false;
    
    // returns 1 if both a and b are true, 0 if a differs from b, and -1 if both a and b are false
    private static int wiredLogic(boolean a, boolean b)
    {
        return (a && b ? 1 : (a == b ? -1 : 0));
    }