View Single Post
  #1 (permalink)  
Old 12-11-2007, 09:02 PM
sondratheloser sondratheloser is offline
Member
 
Join Date: Dec 2007
Location: Philly
Posts: 3
sondratheloser is on a distinguished road
Send a message via AIM to sondratheloser
Array traversal issues
I am trying to make a simple hangman program, and I'm working on getting the framework set up. The problem is, I'm pretty sure, in the if statements in the compare method the int whichletter is supposed to be incremented in each run through the for loop in the main method (8 times, it should be) so that it reads the next letter of the randomly chosen 8-letter word, but I think it's just staying at charAt(0) for some reason.. here is the code..

note: I know there are other problems with it probably, but I'm just kind of stuck on this one right now.

Code:
import java.io.*; import java.util.*; public class stuff {//begin classbody public static void main (String[]args)throws IOException {//BEGIN MAIN String wordchosen = PickRandomWord(); System.out.println(wordchosen); int whichletter =0; for (int cnt1=0; cnt1<8; cnt1++) { String guessedletter=guess(); System.out.println("you guessed " + guessedletter); compare(wordchosen, guessedletter, whichletter); whichletter++; } }//END MAIN public static String PickRandomWord() {//begin pickrandom //words to be picked from String wordchosen; String [] word = new String [5]; word [0] ="aardvark"; word [1] ="trioxide"; word [2] ="chromium"; word [3] ="lungworm"; word [4] ="zombiism"; //generate a random number Random randomword = new Random(); int random = randomword.nextInt( 5 ); wordchosen = word[random]; return wordchosen; }//end pickrandom public static String guess() throws IOException {//BEGIN GUESS BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Please Enter your guess. (all lowercase, please)"); String guessedletter = in.readLine(); return guessedletter; }//END GUESS public static void compare (String wordchosen, String guessedletter, int whichletter) {//BEGIN COMPARISON int right = 0; int wrong = 0; int length = 8; if (guessedletter.charAt(whichletter) == wordchosen.charAt(whichletter)) { right++; } if (guessedletter.charAt (whichletter) != wordchosen.charAt(whichletter)) { wrong++; } System.out.println("You guessed " + wrong + " letters incorrectly;."); System.out.println("You guessed " + right + " letters correctly;."); }//END COMPARISON }//end class body
Help appreciated!
-Sondra
Reply With Quote
Sponsored Links