Results 1 to 4 of 4
  1. #1
    ccart62 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Guessing Game with for loop

    I had to write a guessing game with a for loop as a project for class. It also uses a counter to keep count of your guesses. It will tell you if you are too high or too low. It also has a System.out.println to show you the correct answer so you know that it really works. The problem is, it does not end once you enter the correct anumber. How do I edit the for loop so it will end when userAnswer ==computerNumber?

    import javax.swing.*;
    public class GuessingGameLoops {
    public static void main(String[] args) {
    int computerNumber = (int)(Math.random( ) * 100 + 1);
    System.out.println ("The correct guess would be " + computerNumber);
    for (int count = 1; count <= 100; count ++){
    String response = JOptionPane.showInputDialog(null,
    "Enter a guess between 1 and 100","Guessing Game",3);
    int userAnswer = Integer.parseInt(response);
    JOptionPane.showMessageDialog(null, "Your guess is " + userAnswer + determineGuess (userAnswer, computerNumber)
    + "\nTry number " + count);}

    }
    public static String determineGuess (int userAnswer, int computerNumber) {
    if (userAnswer <= 0 || userAnswer >= 100) {
    return "\nInvalid";}
    else if (userAnswer > computerNumber) {
    return "\nYou are too high";}
    else if (userAnswer < computerNumber) {
    return "\nYou are too low";}
    else {
    return "\nCorrect!";
    }



    }
    }

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You answered your own question.
    userAnswer == computerNumber
    The exit condition of a for loop can be anything, doesn't have to be related to the counter at all:
    Java Code:
    boolean run = true;
    for(int i = 0; run; i++) {
      if(i == 56)
        run = false;
      else
        System.out.println(i);
    }
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    ccart62 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    I have been graded on this assignment already. The instructors comments were:
    "Everything is right except the ending of the program when the user
    guesses correctly. If you adjust your for loop so that it ends
    instead of at:
    count <= 100;
    but when
    userAnswer==computerNumber
    it will then end once the right number is guessed."
    I have tried this several ways, but get errors no matter where I insert this statement.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    It told you exactely where to put the statement. Also, your professor gave you the condition for stopping execution, you need a condition that continues execution.
    Java Code:
    for (int count = 1; userAnswer != computerAnswer; count ++)
    since you want to continue the loop until the user guesses the number. As to why this produces an error, look at where you declared userAnswer, and think about where the declaration should be.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Need help in Guessing Game
    By rose in forum Java Gaming
    Replies: 4
    Last Post: 10-27-2010, 11:43 PM
  2. Guessing Game
    By rose in forum Java Gaming
    Replies: 4
    Last Post: 10-27-2010, 09:00 PM
  3. guessing game help
    By yasmin k in forum AWT / Swing
    Replies: 4
    Last Post: 10-31-2009, 06:37 PM
  4. guessing game using GUI
    By yasmin k in forum New To Java
    Replies: 1
    Last Post: 10-26-2009, 01:13 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
  •