0

だから私はいつも .toLowerCase で問題を抱えていて、それがどのように機能するかについてたくさんの記事、ビデオ、本をチェックしました。友達の冗談としてばかげたゲームを作ってみましたが、明らかにうまくいきません

それを修正する最良の方法は何ですか? toLowerCase() はどのように機能しますか? 簡単な説明ができれば、とてもうれしいです!! :)

「Choice」は静的文字列です。

public static void part1()
        {
            System.out.println("Welcome to Chapter ONE ");
            System.out.println("This is just a simple Left Right options.");
            System.out.println("-------------------------");
            System.out.println("You emerge into a cave like structure, It's seems very weird and creeps you out a little, Yet, You continue on your journey \n You see a that you have reached a 'Dead End' and \n now you have two choices: Either go Left into the weird corner, Or Go Right.. Into the Well-Lit Area.");
            choice = input.next();
            if(choice.toLowerCase()=="left")
            {
                deathPre();
            }
            else if(choice.toLowerCase()=="right")
                {
                    TrFight();
                }
            }

だから、これはうまくいかない部分です(皮肉なことに、最初の部分です)私はこれを機能させるために他の方法を試しました. これが私にとって最も簡単なことでしたが、突然不可能になりました。

助けてください!

ロジック:ユーザーが「左」を入力した場合(どちらの方法でも小文字に変換するため、どちらの場合も問題ありません).ユーザーを「deathPre();」に送信し、「右」を入力した場合は「TrFight」に移動する必要があります(); それ以外の場合は、気にしないエラーが発生します。しかし、機能するには「左」と「右」が必要です

4

3 に答える 3

4

文字列を比較して、.equals()使用することもできます

.equalsIgnoreCase("left")

2 番目のものを使用する場合は、「.toLowerCase()」を使用する必要はありません

編集:

エリックが言ったように、あなたも使うことができます

.trim().equalsIgnoreCase("left")
于 2013-04-11T19:34:57.427 に答える
1

Zim-Zam が既にコメントしたように、演算子equalsではなくを使用して文字列を比較する必要があります。==

if(choice.toLowerCase().equals("right"))
...
else if(choice.toLowerCase().equals("left"))

.toLowerCase()おそらくうまく機能しています。

于 2013-04-11T19:34:40.163 に答える
1

代わりにこれを試す必要があります:

public static void part1()
    {
        System.out.println("Welcome to Chapter ONE ");
        System.out.println("This is just a simple Left Right options.");
        System.out.println("-------------------------");
        System.out.println("You emerge into a cave like structure, It's seems very weird and creeps you out a little, Yet, You continue on your journey \n You see a that you have reached a 'Dead End' and \n now you have two choices: Either go Left into the weird corner, Or Go Right.. Into the Well-Lit Area.");
        choice = input.next();
        if(choice.toLowerCase().equals("left"))
        {
            deathPre();
        }
        else if(choice.toLowerCase().equals("right"))
            {
                TrFight();
            }

2 つの文字列を比較するには、String オブジェクトで equals メソッドを使用します。

于 2013-04-11T19:36:33.243 に答える