Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JAVA Guessing Game Problem

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JAVA Guessing Game Problem

    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


    /* 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);  <-------------------------------------------------This is line 18
          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
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: JAVA Guessing Game Problem

    Cross-posted here. Although cross-posting is allowed, it is appreciated if you provide links to all cross-posts in all cross-posts so that folks don't waste time duplicating effort that has already been performed. We value our time and appreciate it if *you* value our time as well.

Similar Threads

  1. Guessing Game
    By Spark2.0 in forum Object Oriented Programming
    Replies: 4
    Last Post: June 6th, 2012, 12:14 PM
  2. Guessing Game
    By scottey93 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 7th, 2011, 01:50 PM
  3. Java Newbie..need help in guessing game program
    By confupavan in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 16th, 2011, 09:22 AM
  4. JAVA HIGH LOW NUMBER GUESSING GAME
    By Laxman2809 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 12th, 2011, 10:50 AM
  5. im in a hurry!!Help with a programm..java game of guessing a word
    By mr_doctor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:17 AM