表达式的非法开始 Java 布尔值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19052559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:43:42  来源:igfitidea点击:

Illegal Start of Expression Java Boolean?

javaif-statementbooleanbitwise-operators

提问by Tanner Banks

I'm trying to run a Bitwise number comparison and my code keeps coming up with an Illegal start of expression on line 30 of my code with the "if" statement.

我正在尝试运行按位数字比较,并且我的代码不断在我的代码的第 30 行使用“if”语句提出一个非法的表达式开始。

My code reads as so:

我的代码是这样写的:

public class Project7 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        double P = keyboard.nextDouble(); 
        double Q = keyboard.nextDouble();
        double R = keyboard.nextDouble();
        double S = keyboard.nextDouble();
        boolean First_Relation;
        boolean Second_Relation;

        if (P > Q) First_Relation = true;
        if (R < S) Second_Relation = true;

        if (First_Relation = true) & (Second_Relation = true); 
        System.out.println("Given the values for p,q,r, and s the expression "
        + "(p > q) && !(r < s) evaluates to " );
    }
}

回答by Jon Skeet

An ifstatement is of the form:

一个if声明的形式为:

if (condition) statement

You've currently got twobracketed conditions... which also end up assigningvalues, which probably isn't what you want.

您目前有两个括号内的条件......最终也会分配值,这可能不是您想要的。

So first fix to get it to compile:

所以首先修复以使其编译:

if ((First_Relation = true) & (Second_Relation = true))

Then change the assignments to equality checks, as otherwise it will simply assign trueto both variables and the condition will pass regardless of their previous values:

然后将赋值更改为相等检查,否则它将简单地赋值true给两个变量并且条件将通过而不管它们以前的值如何:

if ((First_Relation == true) & (Second_Relation == true))

Then remove comparisons with boolean constants:

然后删除与布尔常量的比较:

if ((First_Relation) & (Second_Relation))

Then remove unnecessary brackets:

然后删除不必要的括号:

if (First_Relation & Second_Relation)

Then make the variables follow Java naming conventions:

然后使变量遵循 Java 命名约定:

if (firstRelation & secondRelation)

Then use the more conventional &&instead of &- &&is short-circuiting, and is almost always what you want:

然后使用更传统的&&而不是&-&&是短路,并且几乎总是你想要的:

if (firstRelation && secondRelation)

Now you've still got a semi-colon directly after your ifcondition, which makes it pointless - it will alwaysexecute the System.out.printlnstatement, because that's not part of the ifstatement. You couldjust remove the semi-colon, but I'd add braces for clarity:

现在你的if条件后面仍然有一个分号,这使它毫无意义——它总是会执行System.out.println语句,因为那不是if语句的一部分。您可以删除分号,但为了清楚起见,我会添加大括号:

if (firstRelation && secondRelation) {
    System.out.println("insert text here");
}

Next, note that you're only actually initializing your variables if the condition is true - so you'll currently get a compile-time error for trying to read variables which aren't definitely assigned.

接下来,请注意,如果条件为真,您实际上只是在初始化您的变量 - 因此您当前会因尝试读取未明确分配的变量而出现编译时错误。

First, fix the definite assignment:

首先,确定明确的分配:

// Names changed to follow conventions
boolean firstRelation = p > q;
boolean secondRelation = r < s;

... and the code above should be fine.

...上面的代码应该没问题。

Next, spot that you're really gaining very little indeed from those extra variables. Inline the conditions instead:

接下来,发现您确实从这些额外变量中获得的收益很少。改为内联条件:

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 ";
}

At this point, it becomes very clear that there's a further bug - because your message talks about !(r < s)but the condition is just r < s. So you need to decide what you want to achieve, and make the code and the message reflect the same thing. Note that you don't finish the message, either. Indeed, you could simplify the whole thing to:

在这一点上,很明显还有一个错误 - 因为你的消息谈论!(r < s)但条件只是r < s. 因此,您需要决定要实现的目标,并使代码和消息反映同一件事。请注意,您也没有完成消息。实际上,您可以将整个过程简化为:

System.out.println("Given the values for p,q,r, and s the expression "
    + "(p > q) && !(r < s) evaluates to " + ((p > q) && !(r < s));

... or whatever you want the expression to actually be.

... 或者任何你想要的表达实际上是什么。

回答by Ashok

if (First_Relation == true && Second_Relation == true)
{
    System.out.println("Given the values for p,q,r, and s the expression "
    + "(p > q) && !(r < s) evaluates to " );
}

and the Effective Way is

有效的方法是

    if (First_Relation && Second_Relation)
    {
        System.out.println("Given the values for p,q,r, and s the expression "
        + "(p > q) && !(r < s) evaluates to " );
    }

回答by Ashok

Try to take

尝试采取

(First_Relation = true) & (Second_Relation = true)

into brackets. And remove the ";" from the end of "if" statement, cause it makes no sense : ";" is considered as a new statement termination (empty statement in your case) and as you didnt provide the scope for "if" statement - it works only for the next statemnt i.e. empty statement.

放入括号中。并删除“;” 从“if”语句的末尾开始,因为它没有意义:“;” 被视为新的语句终止(在您的情况下为空语句),并且由于您没有提供“if”语句的范围 - 它仅适用于下一个语句,即空语句。

回答by shovavnik

As far as I know, you can't use the &operator in Java to perform a bitwisecomparison between doubles. It can only be used with other simpler primitives, like ints and chars.

据我所知,您不能使用&Java 中的运算符在doubles之间执行按位比较。它只能与其他更简单的原语一起使用,如整数和字符。

In addition, the way you're using the &operator will not perform a bitwise comparison between the numbers because you're using it to compare the results of P>Qand R<S, both of which produce boolean values.

此外,您使用&运算符的方式不会在数字之间执行按位比较,因为您使用它来比较P>Qand的结果R<S,这两者都会产生布尔值。

To perform a bitwise comparison between doubles, you need to use a different technique to directly compare P with Q and R with S. Here's an example of one way to do that: https://stackoverflow.com/a/13928322/213343.

要在双精度数之间执行按位比较,您需要使用不同的技术直接将 P 与 Q 和 R 与 S 进行比较。这是一种方法的示例:https: //stackoverflow.com/a/13928322/213343

回答by Lluis Martinez

If the condition is not met then there's no message. I therefore suggest:

如果条件不满足,则没有消息。因此我建议:

boolean evaluation = (P > Q) && !(R < S);
System.out.println("Given the values for p,q,r, and s the expression "
    + "(p > q) && !(r < s) evaluates to " + evaluation);