Input in an if statement
Author |
Message |
username123
|
Posted: Wed Sep 23, 2015 8:07 am Post subject: Input in an if statement |
|
|
I can't get this code to work:
Java: |
String answer;
System. out. println("Do you have $200,000 - $500,000 or more?");
answer = input. next();
if (answer == "yes")
{
System. out. println("Do you want to get to places quickly?");
answer = input. next();
if (answer == "yes")
{
System. out. print("Purchase a Lamborghini Aventador.");
}
else if (answer == "no")
{
System. out. print("Purchase a Rolls-Royce Phantom.");
}
else
{
System. out. print("Invalid answer.");
}
}
else if (answer == "no")
{
System. out. println("Do you have $100,000 - $200,000?");
answer = input. next();
if (answer == "yes")
{
System. out. println("Do you like imports?");
answer = input. next();
if (answer == "yes")
{
System. out. print("Purchase a Nissan GTR.");
}
else if (answer == "no")
{
System. out. print("Purchase a Dodge Viper SRT.");
}
else
{
System. out. print("Invalid answer.");
}
}
else if (answer == "no")
{
System. out. print("Purchase a Honda Fit.");
}
else
{
System. out. print("Invalid answer.");
}
}
else
{
System. out. print("Invalid answer.");
}
|
Whenever I run it, it displays the first question and asks for input, but no matter what I enter, it always outputs "Invalid answer." and stops there. How do I fix this? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
username123
|
Posted: Wed Sep 23, 2015 8:37 am Post subject: Re: Input in an if statement |
|
|
Nevermind, I figured it out. (replaced == with .equals) |
|
|
|
|
![](images/spacer.gif) |
|
|