| | |
Java word guessing game - problem
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 6
Reputation:
Solved Threads: 0
Hi all, I've been working on a Java guessing game, similar to hangman which rewards you with a picture using the Gogga class (developed by the people who wrote my IT textbook for Grade 10, also I removed gogga for this website, since I doubt you'd be able to find it).
Just a word of warning... it could be simplified to a lot less lines of code, but since I may not use for complex things such as arrays, do while, etc. The problem I'm having is at the word guessing part of the program, it asks for the next character and displays the words and picture within the if statements, regardless of whether the input character was incorrect of not (so it carries out the code regardless if the conditions of the "if" is met). I hope you guys know what I'm talking about. Also I seem to not be able to have "else" after the big bunch of if statements. Just for reference, the program is not yet 100% complete since I first wanted to sort out this bug.
Thank you for your time and patience,
- Charl aka Potty391
Just a word of warning... it could be simplified to a lot less lines of code, but since I may not use for complex things such as arrays, do while, etc. The problem I'm having is at the word guessing part of the program, it asks for the next character and displays the words and picture within the if statements, regardless of whether the input character was incorrect of not (so it carries out the code regardless if the conditions of the "if" is met). I hope you guys know what I'm talking about. Also I seem to not be able to have "else" after the big bunch of if statements. Just for reference, the program is not yet 100% complete since I first wanted to sort out this bug.
Java Syntax (Toggle Plain Text)
/* PAT Assignment --- Word Guessing + Number pattern game Charl Potgieter 10Pn October / November 2009 */ import javax.swing.*; import java.util.*; // for random public class GuessingGame1 { public static void main (String [] args) { JOptionPane.showMessageDialog(null, "Welcome to the Guessing Game!"); for ( int j = 1; ; j = j ++ ) { String menu, undscore1, undscore2, undscore3, undscore4, undscore5; //have underscores Random ran = new Random(); char char1, char2, char3, char4, char5, inpChar; String[] options = {"Word Guessing", "Patterns Game ", "Exit" }; // Used to make options to select in the menu menu = (String) JOptionPane.showInputDialog (null, " MENU", " Guessing Game", JOptionPane.PLAIN_MESSAGE, null, options, " "); //Starting menu if (menu.equals (options [0])) { String word = JOptionPane.showInputDialog("Player 1: Please input a word (to a maximum of 5 characters), for single player type: 'random' "); //Ask for input, whether the user wants to make a word, or a random one int rndChoice = (int)(Math.random()*14); // rndChoice is made for the switch to be able to randomly choose a word //Code for user word if (!word.equals("random")) { /* undscore1 = "_"; undscore2 = "_"; undscore3 = "_"; undscore4 = "_"; undscore5 = "_"; */ String inpWord; inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess"); inpChar = inpWord.charAt(0); char1 = word.charAt(0); char2 = word.charAt(1); char3 = word.charAt(2); char4 = word.charAt(3); char5 = word.charAt(4); if (inpChar == char1) { System.out.println("The word is: "+char1+"____ "); } inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess"); inpChar = inpWord.charAt(0); if(inpChar == char2); { System.out.println("The word is: "+char1+char2+"___" ); } inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess"); inpChar = inpWord.charAt(0); if(inpChar == char3); { System.out.println("The word is: "+char1+char2+char3+"__" ); } inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess"); inpChar = inpWord.charAt(0); if(inpChar == char4); { System.out.println("The word is: "+char1+char2+char3+char4+"_" ); } inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess"); inpChar = inpWord.charAt(0); if(inpChar == char5); { System.out.println("The word is: "+char1+char2+char3+char4+char5 ); } JOptionPane.showMessageDialog(null, "Congratulations, you have won! \n Returning to menu"); } //Code for random word if (word.equals("random")) { switch (rndChoice) { case 0: word = "hello"; break; case 1: word = "kevin"; break; case 2: word = "charl"; break; case 3: word = "county"; break; case 4: word = "sweety"; break; case 5: word = "motor"; break; case 6: word = "edges"; break; case 7: word = "works"; break; case 8: word = "place"; break; case 9: word = "right"; break; case 10: word = "dudes"; break; case 11: word = "lucky"; break; case 12: word = "homes"; break; } undscore1 = "_"; undscore2 = "_"; undscore3 = "_"; undscore4 = "_"; undscore5 = "_"; String inpWord; for ( int count = 1; ; count = count ++ ) { inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess"); inpChar = inpWord.charAt(0); char1 = word.charAt(0); char2 = word.charAt(1); char3 = word.charAt(2); char4 = word.charAt(3); char5 = word.charAt(4); if (inpChar == char1) { System.out.println("The word is: "+char1+"____ "); } inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess"); inpChar = inpWord.charAt(0); if(inpChar == char2); { System.out.println("The word is: "+char1+char2+"___" ); } inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess"); inpChar = inpWord.charAt(0); if(inpChar == char3); { System.out.println("The word is: "+char1+char2+char3+"__" ); } inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess"); inpChar = inpWord.charAt(0); if(inpChar == char4); { System.out.println("The word is: "+char1+char2+char3+char4+"_" ); } inpWord = JOptionPane.showInputDialog("Please input a character that you want to guess"); inpChar = inpWord.charAt(0); if(inpChar == char5); { System.out.println("The word is: "+char1+char2+char3+char4+char5 ); } JOptionPane.showMessageDialog(null, "Congratulations, you have won! \n Returning to menu"); } } } if (menu.equals (options [1])) { JOptionPane.showMessageDialog(null, "In this game you will be given 10 sequences of numbers that you will have to complete, you only have one try, its that easy!"); int patrn; int rndNoChoice = (int)(Math.random()*14); // rndNoChoice is made for the switch to be able to randomly choose a pattern switch (rndNoChoice) { case 0: patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 15 ; 30 ; 45 ....")); if (patrn == 60) { JOptionPane.showMessageDialog(null, "Correct!"); } else { JOptionPane.showMessageDialog(null, "Incorrect!"); } break; ///////////////// case 1: patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 10 ; 20 ; 30 ....")); if (patrn == 40) { JOptionPane.showMessageDialog(null, "Correct!"); } else { JOptionPane.showMessageDialog(null, "Incorrect!"); } break; ////////////////// case 2: patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 1 ; 2 ; 3 ....")); if (patrn == 4) { JOptionPane.showMessageDialog(null, "Correct!"); } else { JOptionPane.showMessageDialog(null, "Incorrect!"); } break; ////////////////// case 3: patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 7 ; 14 ; 21 ....")); if (patrn == 28) { JOptionPane.showMessageDialog(null, "Correct!"); } else { JOptionPane.showMessageDialog(null, "Incorrect!"); } break; ///////////////// case 4: patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 8 ; 16 ; 24 ....")); if (patrn == 32) { JOptionPane.showMessageDialog(null, "Correct!"); } else { JOptionPane.showMessageDialog(null, "Incorrect!"); } break; ///////////////// case 5: patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 2 ; 4 ; 6 ....")); if (patrn == 8) { JOptionPane.showMessageDialog(null, "Correct!"); } else { JOptionPane.showMessageDialog(null, "Incorrect!"); } break; ////////////////// case 6: patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 12 ; 24 ; 36 ....")); if (patrn == 48) { JOptionPane.showMessageDialog(null, "Correct!"); } else { JOptionPane.showMessageDialog(null, "Incorrect!"); } break; ////////////////// case 7: patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 1000 ; 750 ; 500 ....")); if (patrn == 250) { JOptionPane.showMessageDialog(null, "Correct!"); } else { JOptionPane.showMessageDialog(null, "Incorrect!"); } break; ////////////////// case 8: patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 4 ; 8 ; 12 ....")); if (patrn == 16) { JOptionPane.showMessageDialog(null, "Correct!"); } else { JOptionPane.showMessageDialog(null, "Incorrect!"); } break; ////////////////// case 9: patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 1000 ; 2000 ; 3000 ....")); if (patrn == 4000) { JOptionPane.showMessageDialog(null, "Correct!"); } else { JOptionPane.showMessageDialog(null, "Incorrect!"); } break; ////////////////// case 10: patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 9 ; 18 ; 27 ....")); if (patrn == 36) { JOptionPane.showMessageDialog(null, "Correct!"); } else { JOptionPane.showMessageDialog(null, "Incorrect!"); } break; ///////////////// case 11: patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 3 ; 6 ; 9 ....")); if (patrn == 12) { JOptionPane.showMessageDialog(null, "Correct!"); } else { JOptionPane.showMessageDialog(null, "Incorrect!"); } break; ///////////////// case 12: patrn = Integer.parseInt(JOptionPane.showInputDialog("What is the next number: \n 23 ; 46 ; 69 ....")); if (patrn == 92) { JOptionPane.showMessageDialog(null, "Correct!"); } else { JOptionPane.showMessageDialog(null, "Incorrect!"); } break; } ////////////////// } if (menu.equals (options [2])) { JOptionPane.showMessageDialog(null,"Thank you for playing!! \n Credits: \t Charl Potgieter"); } } } }
Thank you for your time and patience,
- Charl aka Potty391
![]() |
Similar Threads
- GUI Number Guessing Game Problem (Java)
- Guessing Game (VB.NET)
- Java Word Search Game (Java)
- GUI Python - A Guessing Game (Python)
- Number guessing game problem (C++)
- Number Guessing game problem (C++)
Other Threads in the Java Forum
- Previous Thread: Java hang man, help please.
- Next Thread: Drwaing Circles?
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working





