简体   繁体   English

非法表达式Java布尔值?

[英]Illegal Start of Expression Java Boolean?

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

An if statement is of the form: if语句的形式如下:

if (condition) statement

You've currently got two bracketed conditions... which also end up assigning values, 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 true to 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 if condition, which makes it pointless - it will always execute the System.out.println statement, because that's not part of the if statement. 现在,你仍然有一个分号的后直接if条件,这使得它毫无意义-它将始终执行System.out.println语句,因为这不是的一部分if语句。 You could just remove the semi-colon, but I'd add braces for clarity: 可以删除分号,但为了清晰起见,我会添加括号:

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 . 在这一点上,很明显还有一个错误 - 因为你的消息谈到了!(r < s)但条件只是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: 实际上,您可以将整个过程简化为:

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. ......或者你想要的表达实际上是什么。

As far as I know, you can't use the & operator in Java to perform a bitwise comparison between doubles . 据我所知,你不能使用&操作者在Java中执行双打之间的逐位比较。 It can only be used with other simpler primitives, like ints and chars. 它只能与其他更简单的原语一起使用,如整数和字符。

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>Q and R<S , both of which produce boolean values. 另外,你使用&运算符的方式不会在数字之间进行逐位比较,因为你用它来比较P>QR<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

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

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 : ";" 从“if”语句结束,导致它没有意义:“;” 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 ie empty statement. 被认为是一个新的语句终止(在你的情况下是空语句),因为你没有提供“if”语句的范围 - 它只适用于下一个statemnt即空语句。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM