简体   繁体   English

Java错误消息。 意外类型,必需变量,发现值

[英]Java Error message. unexpected type, required variable, found value

I am doing a debugging exercise for one of my courses. 我正在为其中一门课程进行调试。 I have found and fixed all but one error in the code. 我发现并修复了代码中的一个错误。 I have to fix this one error: 我必须解决这个错误:

int input, x; 
if ((input % 2)= 0)  // <- this is the line with the error
    ++input;
else
    --input;

I am getting an error on the input % 2 , it is saying it is looking for a variable not a value 我在input % 2上遇到错误,这表示它正在寻找变量而不是值

That is because the assignment operator(=) needs a variable to be assigned to the left side of it. 这是因为赋值运算符(=)需要将一个变量分配给它的左侧。 If requires a comparison operator to get a Boolean result. 如果需要比较运算符以获取布尔结果。 You should use == for the same. 您应该使用==来表示相同的内容。 After you resolve this issue, you would face another issue because you are trying to use a local variable (index) without initialisation. 解决此问题后,您将面临另一个问题,因为您尝试使用局部变量(索引)而不进行初始化。

如果((input%2)== 0),则应使用== not =比较/检查值

The problem is the equal statement "=". 问题是等号“ =“。 This "=" should be replaced with "==". 此“ =”应替换为“ ==“。 Here are explained details https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html 这里是详细说明https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

int input, x; 
if ((input % 2) == 0)
 ++input;
 else
 --input;

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

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