-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 に答える