如果语句和 && 给出 IDE 错误 'Syntax error on token "=", <= expected'

If statement and && gives IDE error 'Syntax error on token "=", <= expected'

我有一个简单的猜数字游戏。它具有询问您是否需要小费的功能。它将响应保存在名为 tipsboolean 中,如图所示。

while (run) {
    while (tinvalidrun){
        System.out.println("Do you want any tips? y or n?");
        input=in.next();
        switch(input){
        case "y":
            System.out.println("Ok, we will tell you how close you are!");
            tinvalidrun=false;
            tips=true;
            break;
        case "n":
            System.out.println("Wanna go hard eh? Well, go along!");
            tinvalidrun=false;
            break;
        default:
            System.out.println("You pressed the wrong key!\nDo you have butter fingers?");
        }
    }

然后我还有一些代码可以生成更多变量并生成随机数。最后我得到用户输入的猜测并测试它:

do {
    System.out.println("Enter a number: ");
    guess = in.nextByte();
    times++;
    if (guess == gval) {
        break;
    } else {
        System.out.println("No luck! Try again!");
        if (tips=true) {
            if (guess < gval) {
                  System.out.println("You are too low!");
            }
            else { 
                System.out.println("You are too high!");
            }
        }
    } 

我尝试将 tips=trueguess < gval&& 放在一个 if 中,但它不起作用。我这样做了:

if(guess<gval && tips=true)

它要求我将 = 替换为 <= 或类似的东西。我尝试使用两个 if 语句但是当你说不时,它仍然显示提示。我试着查看括号,但它们看起来不错。请帮忙!如果我可以对我的代码做任何简化或改进或想法,欢迎他们。

我还有更多问题。当您尝试为整数键入一个字母时,它会崩溃(很明显)。我尝试使用字符串和我的 tinvalidrun 循环来避免这种情况,但我做不到。有没有办法做到这一点? 谢谢! :)

测试相等性

您需要 tips == true,或者只需要 if (guess<gval && tips)if (tips),因为您正在测试 boolean

= 是一个 assignment operator, == is an equality operator。两者很容易混为一谈,但差异巨大...

布尔赋值也是一个(意外的)相等性测试

您说“我尝试使用两个 if 语句,但当您拒绝时,它仍然显示提示。”。 if 语句需要一个 must have type boolean. Any assignment expression (i.e. tips = true) evaluates as the new value assigned to the variable. From the JLS §15.26.1 Simple Assignment Operator:

的表达式

At run time, the result of the assignment expression is the value of the variable after the assignment has occurred. The result of an assignment expression is not itself a variable.

因此 if (tips = true) 是有效的语法,因为 tips = true 一个赋值,也是一个 boolean 表达式,因此可以在一个 if

在你的情况下 tips = truetrue 分配给 tips(即使它开始时是 false),然后 returns [=] 的新值26=]。 if 看到新值 (true) 并愉快地继续。因此,tips 最初不是 true 的测试。例如,这意味着以下内容打印出 "Oops!" 文本:

boolean tips = false;
if (tips=true){
    System.out.println("Oops! This tests as true, but that isn't what we wanted");
}

如您所见,将赋值和测试混为一谈通常不是一个好主意。如果在 if/do/while 表达式中使用 = 通常是错误的。有时它可以帮助简洁,但通常是不好的做法。

为什么 if (guess<gval && tips = true) 不起作用?

这是由于 operator precedence。在表达式 if (guess<gval && tips = true) 中,运算符优先级为 < 然后 && 然后 =,因此编译器不再将 tips = true 部分作为 [= 的赋值25=] 提示。您可以使用方括号包含 tips = true,但正如我上面所说的,您并不是真的想要赋值,因此您可以忽略此细节 .

正在测试有效字节输入

对于你问题的最后一部分 - 你说如果有人输入一个字母你会得到一个无效字节。我猜你得到了 java.util.InputMismatchException

您可以使用 Scanner.hasNextByte() 在使用输入之前测试输入是否为字节。如果 returns 为假,您可以使用 nextLine() 使用和丢弃输入,打印出错误消息并再次循环。

根据您编写代码的方式,您可能还看到了来自 eclipse 的消息,例如 "The operator <= is undefined for the argument type(s) boolean, Boolean" 或来自 javac 的消息,例如"error: unexpected type ... required: variable, found: value"。 None 除了你的语法完全混乱编译器无法解析之外,他们告诉你很多。

在这一行

if(tips=true)

=表示赋值

这应该是

if(tips==true)

首先,相等运算符的测试是==而不是=。其次,测试布尔值时不需要运算符。 if (tips) ... 够了。

===是两个完全不同的东西。 = 是赋值运算符,用于为变量赋值。 == 是相等运算符,用于检查变量是否等于某个值,或等于另一个变量。

您应该改用 ==

if(tips = true) 始终是 true 条件,因为 这里 我们有 assignedtruetips

它将首先将提示分配给 true,然后 check 将在 if 中执行,因此您的 else 部分 dead 由于此代码和控件永远不会进入 else 部分。

如果你想检查 tips 是否是 true 那么你可以只写 if(tips) 如果你想检查它是否是 false 比你应该写 if(tips == false)。其中 == 将检查 equality= 将分配值或引用。

有点变化,用==代替=

而不是

if (tips=true)

你会做的

if (tips==true)
  1. 比较是double ==,single是赋值。
  2. 与true比较是冗余(但合法)

if(guess < gval && tips==true)

if(guess<gval && tips)

您的代码片段没有显示变量声明,也许您也比较了错误的类型。我的片段假设提示是 boolean.

一个等号用于分配,就像您在 (guess = in.nextByte();, tinvalidrun=false, tips=true; 中所做的那样..) RHS 变量中的值是 "put" 到 LHS 变量。变得相等,即它们包含相同的值。要进行比较,请使用两个等号运算符、比较运算符 (foo1 == foo2)。 several operators