JAVA跟踪工作正常,答案相同,但在IF语句中不起作用

JAVA跟踪工作正常,答案相同,但在IF语句中不起作用,java,Java,我正在为我的第一个java测试做一个基本的猜测游戏,我发现了一个有趣的问题。我的if语句没有执行它被告知的操作,但是当我跟踪它时,输出是相同的。我的代码是 import java.util.Scanner; public class Game { public static void main(String[] arg) { Scanner GetInput = new Scanner(System.in); try { String Word;

我正在为我的第一个java测试做一个基本的猜测游戏,我发现了一个有趣的问题。我的if语句没有执行它被告知的操作,但是当我跟踪它时,输出是相同的。我的代码是

 import java.util.Scanner;

 public class Game {
 public static void main(String[] arg) {
    Scanner GetInput = new Scanner(System.in);
    try {
        String Word;
        String Guess;
        int attempts = 0;
        boolean win = false;

            System.out
                    .println("Hello, and welcome to the guessing game! This game is very simple; one player will enter a word when");
            System.out
                    .println("asked, then the next player will need to guess the word Be warned player two, you only have three guesses");
            System.out
                    .println("Let's get started. Player ONE, please enter you're word: ");
            Word = GetInput.next();
            System.out
                    .println("Okay the word has been entered, now player TWO must guess");

            do {
                Guess = null;
                System.out.println("Guess the word: ");
                Guess = GetInput.next();
                System.out.println(Guess + "/" + Word);
                if (Word == Guess) {
                    System.out.println("You won!");
                    break;
                } else if (Guess != Word) {
                    attempts ++;
                } 
                if (win == false && attempts == 3) {
                        System.out.println("You lose!");
                        break;
                }
            } while (attempts <= 3);
    } finally {
        GetInput.close();
    }
}
import java.util.Scanner;
公开课游戏{
公共静态void main(字符串[]arg){
扫描仪GetInput=新的扫描仪(System.in);
试一试{
字符串字;
字符串猜测;
int=0;
布尔赢=假;
系统输出
.println(“您好,欢迎来到猜谜游戏!这个游戏非常简单;一个玩家在游戏开始时会输入一个单词”);
系统输出
.println(“如果被问到,那么下一个玩家将需要猜测单词Be warning player two,您只有三次猜测”);
系统输出
.println(“让我们开始吧。玩家一,请输入您的单词:”);
Word=GetInput.next();
系统输出
.println(“好的,已经输入了单词,现在玩家二必须猜测”);
做{
猜测=null;
System.out.println(“猜单词:”);
Guess=GetInput.next();
System.out.println(猜+“/”+字);
如果(单词==猜测){
System.out.println(“你赢了!”);
打破
}else if(猜测!=单词){
尝试++;
} 
if(win==false&&truments==3){
System.out.println(“你输了!”);
打破
}

}while(尝试时,不能使用==比较字符串。请使用equals方法


此代码
Word==Guess
不会比较
Word
Guess
相等对象

谢谢。意识到它需要是Guess.equals(Word)。