You should use ==
instead of =
if (enoughTreats == true) {
System.out.println("There are enough treats for all the hamsters!");
}
else {
System.out.println("Oh no! There aren't enough treats!");
}
Remember that ==
is the comparison operator and =
is the assignment operator.
And as Mike mentioned, just having if(enoughTreats)
will do the trick for you. No need to use ==
operator!
As a matter of fact, you don't need the boolean variable enoughTreats
at all. You could just write your condition like so:
if (treatsPerHamster >= neededTreats) {
// do one thing
}
else {
// do other
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…