-2

I am making a basic guessing game for my first java test and i have same by an interesting problem. My if statement isn't doing what it is told but when i trace it, the outputs are the same. My Code is... ;

 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();
    }
}

}

Rguarascia
  • 193
  • 3
  • 20

1 Answers1

0

you can't use == to compare strings. use equals method

this code Word == Guess will not compare Word and Guess objects for equality

lakshman
  • 2,641
  • 6
  • 37
  • 63