If statement always giving the same answer
import java.util.Scanner;
c开发者_如何转开发lass Practice {
public static void main(String args[]) {
System.out.println("Enter the number of treats you have:");
Scanner treatsScanner = new Scanner(System.in);
int treats = (treatsScanner.nextInt());
System.out.println("Enter the number of hamsters you have:");
Scanner hamstersScanner = new Scanner(System.in);
int hamsters = (hamstersScanner.nextInt());
System.out.println("How many treats does each hamster need?");
Scanner neededTreatsScanner = new Scanner(System.in);
int neededTreats = (neededTreatsScanner.nextInt());
int treatsPerHamster = treats / hamsters;
boolean enoughTreats = treatsPerHamster >= neededTreats;
if (enoughTreats = true) {
System.out.println("There are enough treats for all the hamsters!");
}
else if (enoughTreats = false) {
System.out.println("Oh no! There aren't enough treats!");
}
}
}
Can someone explain to me why this program returns "There are enough treats for all the hamsters!" regardless of whether "neededTreats" > "treatsPerHamster"?
Thank you.
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
}
You are assigning the value true to enoughtreats.
Try using the equality operator rather than assignment:
if (enoughtreats == true) {
...
}
or simply:
if(enoughtreats) {
...
}
In java, the '=' operator assigns a value to a variable. In this case,
if (enoughTreats = true)
assigns the value 'true' to 'enoughTreats' and then checks if 'enoughTreats' is true (which it always will be).
Instead, you want to put
if (enoughTreats == true)
so that it will check if enoughTreats is true or false.
Use ==
for equality, not =
.
if (enoughTreats = true)
By using =
, you are assigning true
to enoughTreats
. Use the ==
comparison operator instead.
You need to change these two statements
if (enoughTreats = true)
else if (enoughTreats = false)
into
if (enoughTreats == true)
else if (enoughTreats == false)
You could also shorten the code and get the exact same effect by simply typing this below:
if (enoughTreats)
else
If you put a boolean variable inside of the parenthesis of an if statement by itself, the if statement will check to see if it's true, thus you wouldn't need to type '== true.' You could also replace the else if statement in your code with a simple 'else' because if the enoughTreats variable is not equal to true, the else code will automatically execute without you having to specifically state a condition.
Few things to note and to add to the listed answers
- Just one scanner is enough
Scanner inputScanner = new Scanner(System.in);
- Braces around
(...Scanner.nextInt());
is not really necessary - You may need to consider a non zero check for
hamsters
! - Handle Non Ints & -ve numbers in the input
else if(){}
is not required whenif(){}
has only single boolean check , just anelse{
is sufficient
精彩评论