-1

I'm currently working on a financial planning app for class but I cant get a loop with a condition inside it to work. It just keeps looping despite the condition - it's almost as if the condition is being ignored completely.

Here's my code - please help!

while (true){
        Scanner scanVar = new Scanner(System.in);
        System.out.println("\nEnter expenditure item: ");
        String myString = scanVar.nextLine();
        Scanner scanVar2 = new Scanner(System.in);
        System.out.println("\nEnter expenditure value: ");
        double myDouble = scanVar2.nextDouble();
        expenditureMap.put(myString, myDouble);
        Scanner scanVar3 = new Scanner(System.in);
        System.out.println("\nAnother item? ");
        String myString2 = scanVar3.nextLine();
            if (myString2 == "yes") {
                continue;
            }
            else {
                break;
            }
    }

Many thanks, Dylan

4

4 に答える 4

1

あなたは本当に使いたいmystring2.equals("yes")(またはもっと良い"yes".equals(mystring2)

オブジェクトの==演算子は、それらが同じ文字列値ではなく、同一のインスタンスであることをテストします....

String a = new String("yes");
String b = new String("yes");

a == b => false
a.equals(b) => true
于 2013-09-18T18:25:22.207 に答える