我正在生成一个从1到100的随机数,并将其显示给用户,然后我将有20次机会猜测该随机数。响应应该是“是”或“否”,但是现在当我运行该程序时,它只是从用户那里获取输入并结束执行,因此不会访问if else if循环
public static void main(String args[])
{
Random rand = new Random();
int rand_int1 = rand.nextInt(100);
System.out.println("Random value is = " + rand_int1);
int n = 20;
Scanner userinput = new Scanner(System.in);
System.out.println("is your number is form 0 to 50 ?");
String ans1 = userinput.nextLine();
System.out.println("ans : " + ans1);
if (ans1 == "yes") {
System.out.println("number is more then 50");
}
else if (ans1 == "no") {
System.out.println("number is less then 50");
}
}
Random value : 23
is your number is more then or equal to 50?
ans : No
稍后,我将添加更多代码,这也将出现在输出中:
is your number is less then or equal to 25?
ans : Yes