我很困惑为什么我不能介入这些if语句中的任何一个。我错过了一些非常简单的东西吗?我输入aa或AA或c或C,它不会进入if语句。有什么想法吗?
while(play){
String answer;
Scanner input = new Scanner( System.in );
System.out.println("Please select an option");
answer = input.nextLine();
answer = answer.toUpperCase().trim();
System.out.println(answer);
if(answer == "AA"){
System.out.println("Have fun!");
System.out.println("Enter player 1's name");
Player player1 = new Player(input.nextLine());
System.out.println("Enter player 2's name");
Player player2 = new Player(input.nextLine());
System.out.println(player1.getName());
System.out.println(player1.getCash());
}
else if (answer == "C"){
play = false;
System.out.println("Thanks for playing");
}
}
答案 0 :(得分:0)
对于比较字符串,您需要使用equals方法。例如:
if(answer.equals("AA")){
请注意,这将比较区分大小写。如果您不想使用区分大小写:
if(answer.equalsIgnoreCase("AA")){