if语句的形式如下:
if (condition) statement
你现在有两个方括号的条件…也最终分配值,这可能不是你想要的.
所以首先要解决它来编译:
if ((First_Relation = true) & (Second_Relation = true))
然后将分配更改为等式检查,否则它将简单地将两个变量赋值为true,并且条件将通过,而不管其以前的值如何:
if ((First_Relation == true) & (Second_Relation == true))
然后删除与布尔常量的比较:
if ((First_Relation) & (Second_Relation))
然后删除不必要的括号:
if (First_Relation & Second_Relation)
然后使变量遵循Java命名约定:
if (firstRelation & secondRelation)
然后使用更传统的&&而不是& – &&&是短路的,几乎总是你想要的:
if (firstRelation && secondRelation)
现在你还有一个分号直接在你的if条件之后,这样就没有意义 – 它总是执行System.out.println语句,因为这不是if语句的一部分.你可以删除分号,但为了清楚起见,我会添加大括号
if (firstRelation && secondRelation) {
System.out.println("insert text here");
}
接下来,请注意,如果条件为真,您只是实际初始化变量,因此您将获得一个编译时错误,尝试读取未明确分配的变量.
首先,修正明确的任务:
// Names changed to follow conventions
boolean firstRelation = p > q;
boolean secondRelation = r < s;
…和上面的代码应该是罚款.
接下来,发现你真的从这些额外的变量中获得的很少.内联条件:
if (p > q && r < s) {
System.out.println("Given the values for p,q,r, and s the expression "
+ "(p > q) && !(r < s) evaluates to ";
}
在这一点上,变得很清楚,还有一个错误 – 因为你的消息谈论!(r
System.out.println("Given the values for p,q,r, and s the expression "
+ "(p > q) && !(r < s) evaluates to " + ((p > q) && !(r < s));
…或者你想要的表达式实际上是什么.