import java.util.Scanner;
public class guessGame {
public static void main(String[] args){
String ansOne;
String ansTwo;
String result="";
Scanner sc = new Scanner(System.in);
System.out.println("TWO QUESTIONS!");
System.out.println("Think of an object and I'll try to guess it");
System.out.println("Qusetion 1) Is it a animal,vegetable or mineral? ");
ansOne = sc.next();
System.out.println("Qusetion 2) Is it bigger than a bread box");
ansTwo = sc.next();
if(ansOne == "animal" && ansTwo == "yes"){
result = "Squirrel";
}
else if( ansOne == "animal" && ansTwo == "no"){
result = "moose";
}
else if( ansOne == "Vegetable" && ansTwo == "Yes"){
result = "carrot";
}
else if( ansOne == "Vegetable" && ansTwo == "no"){
result = "Watermelon";
}
else if( ansOne == "mineral" && ansTwo == "Yes"){
result = "Paper clip";
}
else if( ansOne == "mineral" && ansTwo == "no"){
result = "Camero";
}
System.out.println("My guess is that You are thinking of a " +result);
System.out.println("I would ask you if I'm right,but I don't actually care...");
}
}
在上面的代码中,我想显示结果。但它没有 显示,我得到以下输出:
两个问题! 想想一个物体,我会试着猜测它 问题1)它是动物,植物还是矿物质? 蔬菜 问题2)它是否比面包盒大 是 我的猜测是你在想一个人 我会问你我是否对,但我实际上并不关心...
答案 0 :(得分:1)
我认为你应该使用sc.nextLine()而不是sc.next()。另外使用equals()而不是'=='
答案 1 :(得分:0)
您必须将您的字符串与equals()进行比较
E.g。 String1.equals(" LALALA&#34);
如果使用==进行比较,您将获得意想不到的结果。