If statement 错误的布尔方法?
我对布尔方法有一个问题,说明在注释中,但我不明白:为什么括号后面是“false”?我如何使用if语句来判断返回中的条件是正确的还是错误的?除了这些括号,我不能修改任何其他内容。我完全糊涂了。我理解下面的代码,首先我有条件(a和b)?“然后什么”但在第一段代码中我不明白。这是我的密码: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
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));
}
此返回语句执行三项检查以满足要求:
我原以为这个网站也适合初学者,但正如我所看到的,如果这不是一个超级难的问题:让我们否决它!为了什么人?这个问题应该如何被投票表决?我受不了这些,如果你觉得容易的话,帮帮我。不要投反对票!
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));
}