0

私はプログラミングがあまり得意ではなく、なぜこれが機能しないのかわかりません。私が何を入力しても、それは常にelseステートメントに直接行きます。

    public void pizzaIntro()
    {
    Scanner user_input = new Scanner(System.in);
    String user_command = "null";
    String apology = "I'm sorry it appears there has been some kind of mistake in your order.";
    System.out.println("Welcome to " + cM + " here we strive to deliver excellent services to all our customers!");
    System.out.println("The current prize for pizza is $" + bP + " and an extra $" + tP + " per topping.");
    System.out.println(); System.out.println();
    while(user_command != "exit")
    {
        System.out.print("Would you like toppings?(yes/no):");
        user_command = user_input.next();
        if(user_command.toLowerCase() == "yes")
        {
            System.out.println("Good Eat Your Pizza.");
        }
        else if (user_command.toLowerCase() == "no")
        {
            System.out.println("Well Ok Then!");
        }
        else
        {
            System.out.println(apology);
            System.exit(1);
        }
    }
    pic1.show();
}
4

4 に答える 4

2

文字列を比較するには、equalsメソッドを使用します。==とequalsの違いを知るには、https: //stackoverflow.com/questions/7311451/difference-between-equals-and-instanceofを読んでください 。いつ何を使用するかが明確にわかります。

while(!(user_command.equals("exit")) {
    System.out.print("Would you like toppings?(yes/no):");
    user_command = user_input.next();
    if(user_command.toLowerCase().equals("yes"))
    {
        System.out.println("Good Eat Your Pizza.");
    }
    else if (user_command.toLowerCase().equals("no"))
    {
        System.out.println("Well Ok Then!");
    }
    else
    {
        System.out.println(apology);
        System.exit(1);
    }
}
于 2013-03-08T05:48:23.740 に答える
1

を使用しequalsIgnoreCaseます。より安全です

public void pizzaIntro()
    {
        Scanner user_input = new Scanner(System.in);
        String user_command = "null";
        String apology = "I'm sorry it appears there has been some kind of mistake in your order.";
        System.out.println("Welcome to " + cM
                           + " here we strive to deliver excellent services to all our customers!");
        System.out.println("The current prize for pizza is $" + bP
                           + " and an extra $" + tP + " per topping.");
        System.out.println();
        System.out.println();
        while (!user_command.equalsIgnoreCase("exit"))
        {
            System.out.print("Would you like toppings?(yes/no):");
            user_command = user_input.next();
            if (user_command.equalsIgnoreCase("yes"))
            {
                System.out.println("Good Eat Your Pizza.");
            }
            else if (user_command.equalsIgnoreCase("no"))
            {
                System.out.println("Well Ok Then!");
            }
            else
            {
                System.out.println(apology);
                System.exit(1);
            }
        }
        pic1.show();
    }
于 2013-03-08T05:50:47.977 に答える
1

Javaオブジェクトを"=="と比較しないでください(int、long、doubleなどのプリミティブのみ)。それは読むべきです:

if(user_command.toLowerCase().equals("yes")) {
  ...
}

それ以外の場合は、コンテンツではなく、オブジェクトの場所が同じであるかどうかを確認します。

この特定の例では、代わりにString.equalsIgnoreCase(...)を使用することをお勧めします。

于 2013-03-08T05:51:13.750 に答える
1

equals==の代わりにメソッドを使用してください

user_command.toLowerCase().equals("yes")

Javaでは、==は常に2つの参照を比較するだけです。プリミティブデータ型に使用しても問題ありません。文字列はプリミティブデータ型ではありません。文字列はオブジェクトです。equalsメソッドを使用する必要があります。

あなたのケースではequalsIgnoreCase、ケースを無視する方法を使用することを検討できます。

于 2013-03-08T05:47:34.130 に答える