我正在尝试制作一个基本的“20 个问题”类型的东西来学习如何使用带有布尔比较器(如 &&)的 if 语句。然而,我的“如果”陈述不是,呃,“做”(对不起),即使他们的标准得到满足(据我所知)。
当我编译时,无论我输入什么“答案”,我得到的唯一输出是“我会问你是否正确......” ALA:
Think of an object, and I'll try to guess it!
1. Is it an animal, vegetable, or mineral?vegetable
Is it bigger than a breadbox?yes
I'd ask you if I'm right, but I don't really care
我尝试了谷歌搜索,但我觉得我错过了一些如此基本的东西,以至于我没有看到它。这是代码:
Scanner keyboard = new Scanner(System.in);
String response1, response2;
System.out.println("Think of an object, and I'll try to "
+ " guess it!");
System.out.print("1. Is it an animal, vegetable, or mineral?");
response1 = keyboard.next();
System.out.print("Is it bigger than a breadbox?");
response2 = keyboard.next();
if(response1 == "animal" && response2 == "yes")
{
System.out.println("You're thinking of a moose");
}
if(response1 == "animal" && response2 == "no")
{
System.out.println("You're thinking of a squirrel");
}
if(response1 == "vegetable" && response2 == "yes")
{
System.out.println("You're thinking of a watermelon");
}
if(response1 == "vegetable" && response2 == "no")
{
System.out.println("You're thinking of a carrot");
}
if(response1 == "mineral" && response2 == "yes")
{
System.out.println("You're thinking of a Camaro");
}
if(response1 == "mineral" && response2 == "no")
{
System.out.println("You're thinking of a paper clip");
}
System.out.println("I'd ask you if I'm right, but I don't really care");
在此先感谢任何回复者!