I am looking for a little help. I am sitting at home trying to learn java programming from programmingbydoing.com. I need to make a program called TwentyQuestions. The code is missing something to make it work completely. I have tried to find help other places on the web. That is why the code looks much like other peoples code.
import java.util.Scanner;
public class TwentyQuestions
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
String type, size, guess = "";
System.out.println("TWO QUESTIONS!");
System.out.println("Think of an object, and I'll try to guess it.");
System.out.println("");
System.out.println("Question 1) Is it animal, vegetable or mineral?");
System.out.print("> ");
type = keyboard.next();
System.out.println("");
System.out.println("Question 2) Is it bigger than a breadbox?");
System.out.print("> ");
size = keyboard.next();
System.out.println("");
if (type.equalsIgnoreCase("animal"))
{
if (size == "no")
{
guess = "squirrel";
}
else
{
guess = "moose";
}
}
else if (type.equalsIgnoreCase("vegetable"))
{
if (size == "no")
{
guess = "carrot";
}
else
{
guess = "watermelon";
}
}
else if (type.equalsIgnoreCase("mineral"))
{
if (size == "no")
{
guess = "paper clip";
}
else
{
guess = "Camero";
}
}
if (guess == "")
{
System.out.println("The answers provided are not valid. Please try again.");
}
else
{
System.out.println("My guess is that you are thinking of a " + guess + ".");
System.out.println("I would ask you if I'm right, but I don't actually care.");
}
}
}
The problem is that the program does not care about the comparisons that are in the nested if-statements. Control always goes to the else
and prints that one.