【发布时间】:2018-12-11 15:55:14
【问题描述】:
我目前有一个游戏,用户需要正确输入密码才能玩游戏。但是,每次我输入“游戏”作为用户代码时,它都会打印出再见!,而不是玩游戏。谁能解释一下原因?
public static void secretCode() {
System.out.println("Enter your secret code to play: ");
Scanner passwordInput = new Scanner(System.in);
String userCode = passwordInput.nextLine();
String gameCode = "game";
if (userCode == gameCode) {
System.out.println("Let's begin! Each game costs $50 to play.");
playGame();
System.out.println("Thanks for playing. Goodbye!");
}
if (userCode != gameCode) {
System.out.println("Goodbye!");
}
}
【问题讨论】:
-
尝试在 if 语句中使用 userCode.equals(gameCode) 和 !(userCode.equals(gameCode))
标签: java string if-statement java.util.scanner