带有整数的if语句 [英] if statement with integers
问题描述
我是Java新手.我正在寻找家庭作业方面的帮助.我最初不会发布完整的代码,但我认为它不会帮助我学习.
I'm new at Java. I'm looking for some help with homework. I wont post the full code I was doing that originally but I dont think it will help me learn it.
我有一个使用类的程序.我有一个可以验证选择内容的类,一个有我的设置者和getter者的类,以及一个由教授用该程序的IO编码的类(这是一本addres书)
I have a program working with classes. I have a class that will validate a selection and a class that has my setters and getters and a class that the professor coded with the IO for the program (it's an addres book)
我的主句中有这样的声明
I have a statement in my main like this that says
//create new scanner
Scanner ip = new Scanner(System.in);
System.out.println();
int menuNumber = Validator.getInt(ip, "Enter menu number: ", 1, 3);
if (menuNumber = 1)
{
//print address book
}
else if (menuNumber = 2)
{
// get input from user
}
else
{
Exit
}
如果查看我的if语句 if(menuNumber = 1)
,我会看到一条红线,告诉我不能将int转换为boolean.我以为答案是 if(menuNumber.equals(1))
,但这也给了我类似的错误.
If you look at my if statement if (menuNumber = 1)
I get a red line that tells me I cannot convert an int to boolean. I thought the answer was if (menuNumber.equals(1))
but that also gave me a similar error.
我不是100%可以解决该问题的方法,所以我想寻求帮助.我需要将条目转换为字符串吗?现在,我的验证器看起来像:
I'm not 100% on what I can do to fix it so I wanted to ask for help. Do I need to convert my entry to a string? Right now my validator looks something like:
if (int < 1)
print "Error entry must be 1, 2 or 3)
else if (int > 3)
print "error entry must 1, 2, or 3)
else
print "invalid entry"
如果我将main转换为字符串而不是int,是否还必须全部更改呢?
If I convert my main to a string instead of an int wont I have to change this all up as well?
再次感谢您,我还没有那么出色,我想把大部分的工作都淘汰掉.
Thanks again for helping me I haven't been diong that great and I want to get a good chunk of the assignment knocked out.
推荐答案
if (menuNumber = 1)
应该是
if (menuNumber == 1)
前者将值1分配给 menuNumber
,后者测试 menuNumber
是否等于1.
The former assigns the value 1 to menuNumber
, the latter tests if menuNumber
is equal to 1.
得到不能将int转换为boolean
的原因是Java期望 if(...)
构造中的布尔值-但 menuNumber
是 int
.表达式 menuNumber == 1
返回一个布尔值,这是必需的.
The reason you get cannot convert an int to boolean
is that Java expects a boolean in the if(...)
construct - but menuNumber
is an int
. The expression menuNumber == 1
returns a boolean, which is what is needed.
这是各种语言的常见混淆.我认为您可以设置Java编译器来警告您其他可能发生此错误的情况.
It's a common mix-up in various languages. I think you can set the Java compiler to warn you of other likely cases of this error.
某些语言中使用的一个技巧是反过来进行比较:(1 == menuNumber)
,这样,如果您不小心键入 =
,您将获得一个编译器错误,而不是静默错误.
A trick used in some languages is to do the comparison the other way round: (1 == menuNumber)
so that if you accidentally type =
you will get a compiler error rather than a silent bug.
这被称为 Yoda条件.
在Java中,如果使用 .equals()
方法(不是 ==
)比较对象,则可以使用类似的技巧,其中之一可能是空:
In Java, a similar trick can be used if you are comparing objects using the .equals()
method (not ==
), and one of them could be null:
if(myString.equals("abc"))
如果 myString
为null,则
可能会产生 NullPointerException
.但是:
may produce a NullPointerException
if myString
is null. But:
if("abc".equals(myString))
可以应付,并且如果 myString
为null,则只会返回 false
.
will cope, and will just return false
if myString
is null.
这篇关于带有整数的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!