import java.io.*; //for file import java.util.*; //for scanner public class Lab9 { static String userG = ""; public static void main(String[] args)throws FileNotFoundException{ int wrong = 0; // keeps track of user guesses String word = readwords(); boolean temp = false; String[] words = new String[word.length()]; String[] words2 = new String[word.length()]; //methods to be used userGuess(word, words, words2); printhm(wrong); //introduce user to game System.out.println("Welcome to hangman!"); System.out.println("Begin guessing letters!"); //keep track of wrong user guesses for (int i=0; i < words.length; i++){ words[i] = "-"; } while(wrong < 6 && !checkwon(words)) { temp = add(words, words2); printarray(words); if(temp == false) { wrong++; printhm(wrong); System.out.println("Incorrect guess!" + (6 - wrong) + " more guesses left!"); System.out.println(); }else{ //user guess is correct printhm(wrong); System.out.println("Correct guess! " + (6 - wrong) + " more guesses left!"); } } } //display hangman public static void printhm(int wrong) { //hangman display if(wrong == 0) { System.out.println(); System.out.println(" ------"); System.out.println(" | |"); System.out.println(" |"); System.out.println(" |"); System.out.println(" |"); System.out.println(" |"); } else if (wrong == 1){ System.out.println(); System.out.println(" --------"); System.out.println(" | |"); System.out.println(" 0 |"); System.out.println(" |"); System.out.println(" |"); System.out.println(" |"); } else if (wrong == 2) { System.out.println(); System.out.println(" --------"); System.out.println(" | |"); System.out.println(" 0 |"); System.out.println(" | |"); System.out.println(" |"); System.out.println(" |"); } else if (wrong == 3) { System.out.println(); System.out.println(" --------"); System.out.println(" | |"); System.out.println(" 0 |"); System.out.println("/| |"); System.out.println(" |"); System.out.println(" |"); } else if (wrong == 4){ System.out.println(); System.out.println(" ---------"); System.out.println(" | |"); System.out.println(" 0 |"); System.out.println("/|\\ |"); System.out.println(" |"); System.out.println(" |"); } else if (wrong == 5){ System.out.println(); System.out.println(" ---------"); System.out.println(" | |"); System.out.println(" 0 |"); System.out.println("/|\\ |"); System.out.println("/ |"); System.out.println(" |"); } else if (wrong == 6) { System.out.println(); System.out.println(" ---------"); System.out.println(" | |"); System.out.println(" 0 |"); System.out.println("/|\\ |"); System.out.println("/ \\ |"); System.out.println(" |"); System.out.println( "You lost! Better luck next time!!" ); } } // keep track of wrong guesses public static String readwords() throws FileNotFoundException { Scanner input = new Scanner(new File("hangman.txt")); String[] word = new String[10]; String rword = ""; for(int i = 0; i < word.length; i++){ if (input.hasNext()) { word[i] = input.next(); }else { break; } } Random rand = new Random(); int r = rand.nextInt(9); rword = word[r]; return rword; } public static void userGuess(String y, String[] m, String[] words2){ for (int x = 0; x < y.length(); x++){ if (x + 1 < y.length()) words2[x] = y.substring(x, x + 1); else words2[x] = y.substring(x, y.length()); } } public static boolean add(String[] m, String[] ma) { //find if letters are in the word Scanner console = new Scanner(System.in); String letters = console.next(); boolean found = false; for(int i = 0; i < ma.length; i++) { if(ma [i].equals(letters)){ m[i] = letters; found = true; } } if(found) { return true; } else { return false; } } public static boolean checkwon(String[] array) { boolean haswon=true; for(int i = 0; i < array.length; i++) { if (array[i]=="-") haswon=false; } return haswon; } public static void printarray (String[] array) { for(int i = 0; i < array.length; i++) { System.out.print(array[i]); } } }
All I need help with is figuring out how to print congratulations when the user wins the game. it is very simple, but I am too worn out to figure it out as I've already tried for about an hour. Please help