Code:import java.util.Scanner;//imports a Scanner Class import java.util.Random;//imports a Randomizer public class HangmanMy { public static void main (String[] args){ System.out.println("\t\t\t\t\t************Welcome to Hangman*************");//Intro to the game Scanner input = new Scanner(System.in);//Instalizes a new Scanner called input boolean done = false;//new boolean variable done that is set to false boolean reset = true;//new boolean variable reset that is set to true String guess;//new String Variable guess String guesses = ""; char letter;//new char variable int wrongGuess = 6;//new interger variable wrongGuess that stores the value 6 in it, which is the max wrongGuess the user can have StringBuffer dashes;//new StringBuffer variable dashes while(reset == true){ System.out.println("Choose a category -> places / games / animals / subjects ");//Asks the user what types of word they want String words = wordsToChoose(input.nextLine());//creates a new String and itand chooses the word from the method wordsToChoose from the answer given dashes = makeDashes(words);//makes dashes for each of the letter in the word done = false; while(done == false){//when done is false, it does this System.out.println("Here is your word: " + dashes);//prints out the word in dashes System.out.println("Guesses so far: " + guesses);//the guessess the user made so far System.out.print("enter a guess (letter or word): ");//this asks the user to enter a guess guess = input.next();//takes the answer // process the guess if (guess.length() > 1)//if the length of the answer was more than 1 letter it will do this: { if (guess.equalsIgnoreCase(words))//if the guess was the word System.out.println("You got it, Good Job!!");//it prints out this on the screen and the games ends else //if it dosen't match the word System.out.println("Thats not the word, the word was: " + words);//it prints out this on the screen and the games ends done=true;//now done is true so the loop will stop } else // if the user only puts a letter it will process a single letter guess { letter = guess.charAt(0); guesses += letter; if (words.indexOf(letter) < 0) //if the letter given is not in the word it will do this: { wrongGuess--;//this takes out 1 wrongGuess from six everytime the user gets the letter wrong if((wrongGuess <= 5)&&(wrongGuess >= 1)){ System.out.println("Guesses left: " + wrongGuess);}//prints out this when the wrongGuess are between 5-1 else if(wrongGuess == 0){//when the wrongGuess becomes 0 System.out.println("Not Bad! Unfortunately you're all out of guesses. My word was " + words);// prints out the real word cause there were too many guesses done = true;}//done becomes true and the loop stops } else //if the letter is in the words { matchLetter(words, dashes, letter);// put it in dashes where it belongs if(words.equalsIgnoreCase(dashes.toString()))//if the user fills in the dashes using only letters it will do this System.out.println("\nYOU WIN!!!!!!");//print out you win } } while(done == true) { System.out.println("\nDo you want to play again?"); String answer = input.next(); if(answer.equalsIgnoreCase("no")){ System.out.println("GOOD-BYE, THANKS FOR PLAYING"); break;} if(answer.equalsIgnoreCase("yes")){ reset = true; break;} } } } } public static void matchLetter(String words, StringBuffer dashes, char letter) { for (int index = 0; index < words.length(); index++) if (words.charAt(index) == letter) dashes.setCharAt(index, letter); System.out.print("Good guess: "); } public static StringBuffer makeDashes(String s) { StringBuffer dashes = new StringBuffer(s.length()); for (int count=0; count < s.length(); count++) dashes.append('-'); return dashes; } public static String wordsToChoose(String category) { Random generator = new Random(); String name = ""; if (category.equalsIgnoreCase("places")) { String places[] = {"Barcelona", "London", "Paris", "Toronto", "Vancouver", "Frankfurt", "Sydney", "Florida"}; name = places[generator.nextInt(8)]; } else if (category.equalsIgnoreCase("games")) { String games[] = {"BoderLands", "Halo", "FIFA", "GTA", "HITMAN", "Skyrim", "Minecraft", "Zombieville"}; name = games[generator.nextInt(8)]; } else if (category.equalsIgnoreCase("animals")) { String animals[] = {"eagle", "tiger", "lion", "Jaguar", "rhinoceros", "sharks", "dolphin", "whale"}; name = animals[generator.nextInt(8)]; } else if (category.equalsIgnoreCase("subjects")) { String subjects[] = {"Physics", "Tech", "Chemistry", "Gym", "English", "Religion", "Biology", "Math"}; name = subjects[generator.nextInt(8)]; } return name.toLowerCase(); } }
My Second While Loop won`t work, what i mean is that when i say yes to play again it dosen`t reset,
could u please help


Reply With Quote

Bookmarks