简体   繁体   English

一元运算符的坏操作数类型int! 在while声明中

[英]Bad operand type int for unary operator '! in while statement

import java.util.Scanner;
public class GuessingGame_v1
{
    public static void main(String[] args)
    {
        double randNum = Math.random();

        int number =(int) (randNum * 100.0);
        int counter = 0;
        Scanner in = new Scanner(System.in);

        int guess = 0;

        while (!guess = randNum)
        {
            System.out.println("Enter your guess: ");
            int guess = in.nextInt();

           if(guess > randNum)
               System.out.println("Too High");
           else
               System.out.println("Too Low");
        }    
        if (guess = randNum)
            System.out.println("Congradulations you guessed the number!");
    }
}

I am new to code but on this code it keeps saying "bad operand type int for unary operator '!'. What can I do to fix this? 我是新手代码但是在这段代码上它一直说“一元运算符的坏操作数类型int!”。我该怎么做才能解决这个问题?

! is an unary negation operator that expects a single boolean operand. 是一个一元的否定运算符,它需要一个boolean操作数。 Therefore it can't be applied on an int . 因此它不能应用于int

You should change 你应该改变

while (!guess = randNum)

to

while (guess != randNum)

!= is the operator that checks whether two numbers are not equal to each other. !=是检查两个数字是否彼此不相等的运算符。

In addition 此外

if (guess = randNum)

should be 应该

if (guess == randNum)

since you want to compare the numbers (and not to assign randNum to the guess variable). 因为你想比较数字(而不是将randNum分配给guess变量)。

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

相关问题 需要 Java 运算符的帮助,得到“一元运算符的错误操作数类型 int”错误 - Need a bit of assistance with Java operators, getting a “bad operand type int for unary operator” error 一元运算符惊叹号的错误操作数类型动作侦听器 - bad operand type actionlistener for unary operator exclamation mark 这是什么意思“一元运算符++的错误操作数类型字符串” - What does this mean "bad operand type String for unary operator ++" 二进制运算符“ *”的错误操作数类型如何将类型转换为int - bad operand types for binary operator '*' how to covert the type to int 二元运算符的坏操作数类型 - Bad operand type for binary operator 二进制运算符“ *”的错误操作数类型 - Bad operand type for Binary operator “*” 二元运算符'^'的坏操作数类型 - Bad operand type for binary operator '^' 二进制运算符的错误操作数类型+第一类型int []和第二类型int - Bad operand type for binary operator + first type int[] and second type int 如何修复错误“二元运算符 '>=' 第一种类型的错误操作数类型:int[] 第二种类型 int” - How to fix the error "Bad Operand Types for Binary Operator '>=' first type: int[] second type int" 二元运算符“&”的错误操作数类型第一种类型:int[] 第二种类型:int - Bad operand types for binary operator ‘&’ First type: int[] Second type: int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM