-1
import java.util.Scanner;

public class NewClass {
    public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter your name");
        String name = scanner.nextLine();
        System.out.println("Is "+ name + " really your name?");
        String answer1 = scanner.nextLine();
        if (answer1 == "yes"){
        System.out.println("Alright ");
        }else  {System.out.println("Liar!");
    }
    }
}

Liar!即使我输入了它也会输出yes,所以answer1等于yes。为什么?

4

1 回答 1

4

因为==您正在测试参考平等,而不是价值平等。很好的阅读。

而是answer1 == "yes"answer1.equals("yes").

于 2013-10-09T20:26:54.323 回答