I think you should design your application.
Get a pencil, a rubber and some paper and start making a Nassi-Schneiderman diagram where you invoke methods like hangMore(), correctInput() or kill() without entering in low level code.
If you do that you are not gonna be puzzled by loops.
> I think you should design your application.
> Get a pencil, a rubber and some paper and start
Before we start snickering, let's remember today is cultural sensitivity day!
An Indian friend of mine at university told me a classic story of this:
he was at the campus store and asked, "where can I buy a rubber?
The one I have is old and worn out because I'm so tough on it."
> making a Nassi-Schneiderman diagram where you invoke
> methods like hangMore(), correctInput()
Who that? UML not good enough for you ;-)
[url #" style="display: block; background-image: url('http://upload.wikimedia.org/wikipedia/en/thumb/1/17/Multiple_Branching.JPG/800px-Multiple_Branching.JPG'); width: 800px; height: 354px] [/url]
Freaky...
> > I think you should design your application.
>
> > Get a pencil, a rubber and some paper and start
>
> Before we start snickering, let's remember today is
> cultural sensitivity day!
Read here for relevant information about rubbers: http://ftvdb.bfi.org.uk/sift/title/114070
> I kind of do. My teacher told me the same thing. Only
> thing is, when I tried to do it, I got the same
> messed up results.
Merely putting the code into seperate methods will not fix the results. It will make it easier to fix the results.
And I agree about getting some design in place. Close all your code and write it out on paper. Think about what behaviour a hangman game will need, and then write those methods. That's a better approach than just taking your existing code and randomly breaking it into sub-parts a(), b(), and c().
Here is what I would do:
~Create a class (ex: WordChooser ) which chooses a random word from a list.
~Create a method in another class ( WordChecker ) which runs the letter entered by the user and compares it to the word chosen by the WordChooser class, looking for a similar letter.
Add 2 more methods in the WordChecker class to make arrays for right and wrong letter guesses. Make the right guess array which checks the # of characters in word chosen by WordChooser. Then start off with everything = "_". Once a correct letter is guessed, it would find the correct array # to change to the right letter guessed, then this would be accessed by the HangmanDisplayer class and printed into the terminal.
The wrong guess array would be much easier, just tack on wrong letter guesses and access it with the HangmanDisplayer.
~Create the main class HangmanDisplayer, it will contain all of the hanger's beautiful ASCII art, ready to be printed to the terminal.
Doing that will allow you to expand the word list, and even probably add a GUI and Java2D in the HangmanDisplayer class. I bet your teacher will like the fact that you are using 3 classes too.
Then perhaps your need to go back and review basics of Objects. Here's a quick and simple example.
class Foo {
public void execute() {
Bar b = new Bar();
b.run();
}
public static void main(String[] args) {
Foo f = new Foo();
f.execute();
}
}
class Bar {
public void run() {
System.out.println("Hello World");
}
}