java 我收到错误 int 无法转换为布尔值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31257426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I get the error int cannot be converted to boolean?
提问by Omar Baz
So here is my code, it's very simple I'm kinda learning java and reached the part of if/else statements. But every time I try to run this code, I get the error that int cannot be converted to boolean. Also, if anyone knows a good Java tutorial, that would help a lot. This is the code:
所以这是我的代码,它非常简单,我正在学习 Java 并达到了 if/else 语句的部分。但是每次我尝试运行此代码时,都会收到 int 无法转换为 boolean 的错误。另外,如果有人知道一个好的 Java 教程,那会很有帮助。这是代码:
package learn;
public class Learn {
public static void main(String[] args) {
int user = 19;
if (user => 19){
System.out.println("You are 19!");
}
else{
System.out.println("You are not 19!");
}
}
}
回答by DripDrop
The Java greater than or equal tooperator is >=, not =>.
Java大于或等于运算符是 >=,而不是 =>。
Where you did:
你在哪里做的:
if (user => 19) ...
You should have done:
你应该这样做:
if (user >= 19) ...
If you ever have questions regarding this, check the Java Documentationfirst.
如果您对此有任何疑问,请先查看Java 文档。
回答by ALD
It should be written like this:
应该这样写:
public static void main(String[] args) {
int user = 19;
if (user == 19){
System.out.println("You are 19!");
}
else{
System.out.println("You are not 19!");
}
}
You had the operator wrong in the if statement.
您在 if 语句中操作符错误。
回答by cowboy6
The mistake lies in the fact that in java greater than or equal sign is written like this>=
not like this =>
错误在于java中大于或等于号是这样写的>=
而不是这样写的=>
回答by bart
There is a typo in the '>=' relational operator.
'>=' 关系运算符中有一个拼写错误。
x >= y
Greater than or equal to: True if x is greater than or equal to y, otherwise false.
大于或等于:如果 x 大于或等于 y,则为真,否则为假。
public static void main(String[] args) {
int user = 19;
if (user >= 19) {
System.out.println("You are 19!");
}
else {
System.out.println("You are not 19!");
}
}
You are 19!
你才19岁!
public static void main(String[] args) {
int user = 16;
if (user >= 19) {
System.out.println("You are 19!");
}
else {
System.out.println("You are not 19!");
}
}
You are not 19!
你不是19岁!