Results 1 to 3 of 3
  1. #1
    xemkruz2012 is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default JAVA Guessing Game

    Hi! I've been given a guessing game to make on some already written codes by my advisor but I'm really stuck.
    This what I was told to do and couldn't solve!
    --In this guessing game, the user should be prompted to type in a number as long as long as the user’s answer doesn’t match the number generated by the computer.
    --To adjust this program further, adjust and add lines to “count” the number of guesses that a user has taken. Create an integer variable called count to keep track of how many
    guesses a user has taken. This can be done in the main method.
    --Each time a user guesses, we will need to add 1 to the count variable.
    -- Display the number of guesses each time you tell the user whether their guess is correct, incorrect, or invalid. This should be added to the JOptionPane message box that is
    displayed in the main method.


    here is my code: it shows error at line 18! Please help


    Java Code:
    /* This is a Java program to allow a user to guess the computer 1 to 100.
     * Adnan Alvee
     * 10/18/12
     * JDK 1.6
     */
    
    
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    
    
    public class GuessingGame {
    
        public static void main(String[] args) {
           
          int computerNumber = (int) (Math.random()*100 +1); //generates a random number from 1 to 100.
          System.out.println("The correct guess would be " + computerNumber);
           int userAnswer = Integer.parseInt(response);
          do {
          String response = JOptionPane.showInputDialog(null,
                  "Enter a guess between 1 and 100", "Guessing Game", 3); }
          while (userAnswer != computerNumber);
          
          
          JOptionPane.showMessageDialog(null, "Your guess is " + determineGuess(userAnswer, computerNumber)
                                        + "\nTry number: " ); 
          
          
              }
        
        public static String determineGuess(int userAnswer, int computerNumber) {
           
            if (userAnswer <=0 || userAnswer>=100) {
                return "invalid";
            }
            else if (userAnswer == computerNumber) {
            return "correct";
        }
            else if (userAnswer < computerNumber) {
                return "Your guess is too low";
                        }
            else if (userAnswer > computerNumber) {
                return "Your guess is too high";
                        }
            else  {
                return "incorrect";
            } 
            
        } //ends determineGuess method
    } //ends program

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Jingoism is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Re: JAVA Guessing Game

    Java Code:
    int userAnswer = Integer.parseInt(response);
          do {
          String response = JOptionPane.showInputDialog(null,
                  "Enter a guess between 1 and 100", "Guessing Game", 3); }
          while (userAnswer != computerNumber);
    Response has not been created until you enter the loop!

    This would be the right way...

    Java Code:
    do {
    			String response = JOptionPane.showInputDialog(null,
    					"Enter a guess between 1 and 100", "Guessing Game", 3); 
    			userAnswer = Integer.parseInt(response);
    		}
    		while (userAnswer != computerNumber);
    That fixes the error, and it runs... the rest you can iron out.

Similar Threads

  1. guessing game
    By MrM in forum New To Java
    Replies: 1
    Last Post: 02-02-2011, 08:51 PM
  2. Need help in Guessing Game
    By rose in forum Java Gaming
    Replies: 4
    Last Post: 10-27-2010, 11:43 PM
  3. Guessing Game
    By rose in forum Java Gaming
    Replies: 4
    Last Post: 10-27-2010, 09:00 PM
  4. Java - number guessing game
    By kev670 in forum Java Applets
    Replies: 3
    Last Post: 10-22-2010, 01:55 AM
  5. guessing game help
    By yasmin k in forum AWT / Swing
    Replies: 4
    Last Post: 10-31-2009, 06:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •