-1

So I was writing a Java program try to do a hangman program, I was able to make the most of its part working, but at the end when I ask for user input to confirm to play the game again or not, when user input "y" or "Y", the console responded nothing, I really can't figure it out which part I did wrong. Please help me a bit

Thanks

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
import java.util.Vector;

public class Hangman {
    public static void main(String[] args) throws IOException {
        String result="y";
        
        while(result == "y" || result == "Y") {
            
        start();
        System.out.println("Do you want to guess another word? Enter y or n>");
        Scanner input1 = new Scanner(System.in);
         result = input1.next();
        } 
        System.exit(0);
        }
    public static void start() throws IOException {
         Scanner sc = new Scanner(System.in);
            Vector<String> tries = new Vector<>();
            Random rnd = new Random();
            int misses = 0;

            File file = new File("Hangman.txt");
            if(!file.exists()) {
                System.out.println("File not exist");
                System.exit(0);
            }
            Vector<String> words = new Vector<>();
            Scanner input = new Scanner(file);
            while (input.hasNext()) {
                String s = input.next();
                words.add(s);
            }
            input.close();
            

            String answer = words.get(rnd.nextInt(words.size()));
            String guess = "";
            for (int i = 0; i < answer.length(); i++) {
                guess += '*';
            }
            
            while(guess.indexOf("*")!=-1) {
                 System.out.print("(Guess) Enter a letter in word "+ guess + " > ");
                 String takeguess = sc.next();
                 if(tries.isEmpty()) {
                     tries.add(takeguess);
                     if(answer.indexOf(takeguess)!= -1) {
                         String copy = guess;
                         guess ="";
                         for (int i = 0; i< answer.length();i++) {
                             if (answer.charAt(i) == takeguess.charAt(0)) {
                                 guess += answer.charAt(i);
                             } else {
                                 guess += copy.charAt(i);
                             }
                         }
                     }else {
                         misses++;
                         System.out.println("        " + takeguess + " is not in the word");
                     }
                 }else {
                     boolean repeat = false;
                     for (String s : tries) {
                         if(s.equals(takeguess)) {
                             repeat = true;
                         }
                     }
                     if(repeat) {
                         System.out.println("        " + takeguess + " is already in the word");
                     }else {
                         tries.add(takeguess);
                         if(answer.indexOf(takeguess)!=-1) {
                             String copy = guess;
                             guess = "";
                             for (int i = 0; i < answer.length(); i++) {
                                 if (answer.charAt(i) == takeguess.charAt(0)) {
                                     guess += answer.charAt(i);
                                 } else {
                                     guess += copy.charAt(i);
                                 }
                             }
                         }else {
                             misses++;
                             System.out.println("        " + takeguess + " is not in the word");
                         }
                     }
                 }
            }
            System.out.println("The word is "+ answer +". You missed "+misses+" time");
            System.out.print("Enter a new word to be added in the memory> ");
            String word = "";
            word += sc.next();
            BufferedWriter bw;
            bw = new BufferedWriter(new FileWriter(file, true));
            bw.write(" "+ word);
            bw.close();
            
    }
}
于雅健
  • 21
  • 1

1 Answers1

0

The issue comes from using "==" operator. This operator is used to compare object locations. You are attempting to compare the contents of the objects, so you will want to use .equals() method. Check this out https://www.geeksforgeeks.org/difference-equals-method-java/

Blake
  • 24
  • 1