Okay, so I'm in high school and taking a mandatory Computer Programming class. I have to create a hangman program that the user can play over and over again using different words. However, whenever the user plays again, it just uses the same word from before. Can anyone help me?
import java.util.*;
import java.io.*;
import java.net.*;
public class Hangman
{
public static String getRandomWord()
{
try
{
URL page = new URL("http://projecteuler.net/project/words.txt");
BufferedReader br= new BufferedReader(new InputStreamReader(page.openStream()));
String pageSource=br.readLine();
pageSource=pageSource.replace("\"","");
String[] words=pageSource.split(",");
int randNum=(int)(Math.random()*words.length);
String word=words[randNum];
word=word.toLowerCase();
return word;
}
catch(Exception e)
{
return "hippopotamus";
}
}
public static String Masquerade(String Word, String UsedLetters)
{
String NewWord= "";
for (int i=0;i<Word.length();i++)
{
String letter = Word.substring(i,i+1);
if (UsedLetters.contains(letter))
{
NewWord+=letter;
}
else
{
NewWord = NewWord+" _ ";
}
}
NewWord=NewWord.replace(" ", " ");
NewWord=NewWord.trim();
return NewWord;
}
public static String Noose(int Miss)
{
if(Miss==1)
{
System.out.println("\n _______ ");
System.out.println(" | | ");
System.out.println(" | ('_') ");
System.out.println(" | ");
System.out.println(" | ");
System.out.println(" | ");
System.out.println(" | ");
System.out.println(" | ");
System.out.println("__|_______ \n");
}
if(Miss==2)
{
System.out.println("\n _______ ");
System.out.println(" | | ");
System.out.println(" | ('_') ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | ");
System.out.println(" | ");
System.out.println("__|_______ \n");
}
if(Miss==3)
{
System.out.println("\n _______ ");
System.out.println(" | | ");
System.out.println(" | ('_') ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | / ");
System.out.println(" | / ");
System.out.println("__|_______ \n");
}
if(Miss==4)
{
System.out.println("\n _______ ");
System.out.println(" | | ");
System.out.println(" | ('_') ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | / \\ ");
System.out.println(" | / \\ ");
System.out.println("__|_______ \n");
}
if(Miss==5)
{
System.out.println("\n _______ ");
System.out.println(" | | ");
System.out.println(" | ('_') ");
System.out.println(" | \\ | ");
System.out.println(" | \\| ");
System.out.println(" | | ");
System.out.println(" | / \\ ");
System.out.println(" | / \\ ");
System.out.println("__|_______ \n");
}
if (Miss==6)
{
System.out.println("\n _______ ");
System.out.println(" | | ");
System.out.println(" | ('_') ");
System.out.println(" | \\ | / ");
System.out.println(" | \\|/ ");
System.out.println(" | | ");
System.out.println(" | / \\ ");
System.out.println(" | / \\ ");
System.out.println("__|_______ \n");
}
if (Miss==7)
{
System.out.println("\n _______ ");
System.out.println(" | | ");
System.out.println(" | (x_x) ");
System.out.println(" | \\ | / ");
System.out.println(" | \\|/ ");
System.out.println(" | | ");
System.out.println(" | / \\ ");
System.out.println(" | / \\ ");
System.out.println("__|_______ \n");
}
return "";
}
public static void main(String[] args)
{
String Word=getRandomWord();
int Miss=0;
String UsedLetters="";
for (int i=0;i<100;i++)
{
Scanner Maryann=new Scanner(System.in);
System.out.println("Guess a letter: ");
String Input=Maryann.nextLine();
String Guess=Input.toLowerCase();
UsedLetters+=Guess;
System.out.println(Masquerade(Word, UsedLetters));
if (Word.contains(Guess))
{
Miss+=0;
}
else
{
Miss+=1;
}
Noose(Miss);
if (Miss>=7)
{
System.out.println("You just lost the game.");
System.out.println("The word was "+Word+".");
System.out.println("Care to play again? Type y for yes, and n for no.");
String Response=Maryann.nextLine();
if(Response.equals("y"))
{
Miss=0;
UsedLetters="";
getRandomWord();
}
else if (Response.equals("n"))
{
break;
}
else
{
System.out.println("Type y for yes, and n for no.");
}
}
if(!Masquerade(Word, UsedLetters).contains("_"))
{
System.out.println("Congratulations! You won!");
System.out.println("The word was "+Word+".");
System.out.println("Care to play again? Type y for yes, and n for no.");
String Response=Maryann.nextLine();
if (Response.equals("y"))
{
Miss=0;
UsedLetters="";
getRandomWord();
}
else if (Response.equals("n"))
{
break;
}
else
{
System.out.println("Care to play again? Type y for yes, and n for no.");
}
}
}
}
}