-1

可能重复:
如何比较 Java 中的字符串?

我对下面的代码有问题;问题是在你第一次不说地图之后No try again,无论你是否输入“地图”,它都会随着输入框不断重复。

 System.out.println("My cousin Diego is in trouble in the Majestic palace. But how do we get there?");
 System.out.println("Who do we call when we don't know where to go?         Huh? I didn't get that...");
 imTheMap=robotMagic.next();
 if (imTheMap.equals("map"))
 {
    System.out.println("That's right!"); //the issue is here
 }
 else 
 {
    while(imTheMap!=("map"))
    {
        System.out.println("No! try again");
        imTheMap=robotMagic.next();
    }
 }
4

1 回答 1

6

通过改变

 while(imTheMap!=("map"))

 while(!imTheMap.equals("map"))

你总是应该使用 equals() 方法来检查字符串是否相等。就像你的 if 语句一样。

于 2013-01-10T17:15:06.100 回答