[英]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.