Results 1 to 3 of 3
- 11-14-2009, 04:32 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
Need Some Help, Simple Loop For Hangman Game
Hey guys,
So im trying to program a very simple version of Hangman, I plan on making it a bit more complicated than this but so far ive got a bit stuck.
This is my code, I know its absolutely hideous but I was wondering if anyone could tell me why it doesnt actually work?Java Code:import javax.swing.*; import java.lang.String; public class Hangman { public static void main(String[] args) { int count = 0; String word = "bureau"; String[] user={"-","-","-","-","-","-"}; char guess; do { String input = JOptionPane.showInputDialog ("Please enter a character, you current progress is " + user[0] + user[1] + user[2] + user[3] + user[4] + user[5]); guess = input.charAt(0);} while(count < 9); { if(guess == word.charAt(0)); { user[0] = "b"; count++; } if(guess == word.charAt(1)); { user[1] = "u"; count++; } if(guess == word.charAt(2)); { user[2] = "r"; count++; } if(guess == word.charAt(3)); { user[3] = "e"; count++; } if(guess == word.charAt(4)); { user[4] = "a"; count++; } if(guess == word.charAt(5)); { user[5] = "u"; count++; } } JOptionPane.showMessageDialog(null,"Sorry you lose"); } }
The problem is that count doesnt seem to be increasing and the String[] user={"-","-","-","-","-","-"}; array doesnt seem to be updating with the "new" letters.
If anyone could help id really appreciate it.
- 11-14-2009, 07:40 PM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
First of all you should declare a char[] using single quotes {'-', '-', '-' ...etc} not a string array also your if statements should be inside the while loop, or you should make another method. Also, after while(count < 9); you have an open bracket which doesnt need to be there (and its close bracket). Another thing, after your if (guess == word.charAt(0)) you have a semi-colon, this makes your if statement not do anything.
Here is the code you should use, examine it and find out where you went wrong:
Java Code:import javax.swing.*; import java.lang.String; public class Hangman { public static void main(String[] args) { int count = 0; String word = "bureau"; char[] user={'-', '-', '-', '-', '-', '-'}; char guess; do { String input = JOptionPane.showInputDialog ("Please enter a character, you current progress is " + user[0] + user[1] + user[2] + user[3] + user[4] + user[5]); guess = input.charAt(0); count++; if(guess == word.charAt(0)) { user[0] = 'b'; } if(guess == word.charAt(1)) { user[1] = 'u'; } if(guess == word.charAt(2)) { user[2] = 'r'; } if(guess == word.charAt(3)) { user[3] = 'e'; } if(guess == word.charAt(4)) { user[4] = 'a'; } if(guess == word.charAt(5)) { user[5] = 'u'; } } while(count < 9); JOptionPane.showMessageDialog(null,"Sorry you lose"); } }
- 11-14-2009, 07:51 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
[help] game loop
By Fuzzier in forum Java AppletsReplies: 3Last Post: 10-31-2009, 08:02 AM -
Hangman Game..
By iPetey in forum New To JavaReplies: 4Last Post: 05-07-2009, 02:24 PM -
Need help with hangman game
By kurt in forum New To JavaReplies: 4Last Post: 04-25-2009, 06:47 PM -
Hangman Game
By L23 in forum New To JavaReplies: 8Last Post: 07-03-2008, 01:56 PM -
Create the game Hangman
By barney in forum New To JavaReplies: 1Last Post: 08-06-2007, 06:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks