Guessing game with loops

Questions/Tutorials for any programming language not covered in other sections.
5 posts Page 1 of 1
Contributors
User avatar
Jessica
x Girl Power x
x Girl Power x
Posts: 35
Joined: Thu Feb 21, 2013 1:52 am

Guessing game with loops
Jessica
Code: Select all
/* Jessica Chen
 * Wednesday 6/17/13
 * Java 1.7.0_21 and NetBeans 7.3
 * This program will allow a user to guess the computer number 1 to 100
 */
import javax.swing.JOptionPane;

public class GuessingGameLoop {
    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; userAnswer != computerNumber; 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 "
                + determineGuess(userAnswer, computerNumber) + "\nGuesses: "
                + count);
            }
        }
    
    public static String determineGuess (int userAnswer, int computerNumber){
        
        if (userAnswer <= 0 || userAnswer >= 100) {
            // if the number is below 0 or above 100, it is invalid
            return "invalid"; 
        }
        else if (userAnswer == computerNumber) {
            return "correct";
        }
        else if (userAnswer > computerNumber) {
            return "too high";
        }
        else if (userAnswer < computerNumber) {
            return "too low";
        }
        else {
            return "correct";
        }
    }

}
Error is on userAnswer in userAnswer != computerNumber;. Says it cannot find the symbol. I need the looping to end when the user guesses the correct number.

I know that the compiler doesn't know what userAnswer means, so I need to tell it what it is. However I am completely stuck on how to fix this error. What needs to be changed?
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Re: Guessing game with loops
XTechVB
Doesn't java have the While loop try using that instead of the for loop. For example:
Code: Select all
            while(userAnswer == computerNumber)
            {
                MessageBox.Show("You guessed the number!");
                break;
            }
You can find me on Facebook or on Skype mihai_92b
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Re: Guessing game with loops
Shim
here you go
Code: Select all
/* Jessica Chen
* Wednesday 6/17/13
* Java 1.7.0_21 and NetBeans 7.3
* This program will allow a user to guess the computer number 1 to 100
*/
import javax.swing.JOptionPane;

public class GuessingGameLoop{

	private static int userAnswer;

	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;  userAnswer != computerNumber; 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 "
                + determineGuess(userAnswer, computerNumber) + "\nGuesses: "
                + count);
            }
        }
    
    public static String determineGuess (int userAnswer, int computerNumber){
        
        if (userAnswer <= 0 || userAnswer >= 100) {
            // if the number is below 0 or above 100, it is invalid
            return "invalid"; 
        }
        else if (userAnswer == computerNumber) {
            return "correct";
        }
        else if (userAnswer > computerNumber) {
            return "too high";
        }
        else if (userAnswer < computerNumber) {
            return "too low";
        }
        else {
            return "correct";
        }
       
    }

}
Find my programs on Softpedia
User avatar
Jessica
x Girl Power x
x Girl Power x
Posts: 35
Joined: Thu Feb 21, 2013 1:52 am

Re: Guessing game with loops
Jessica
That doesn't end the loop when the correct number is guessed (unless I misunderstood...?)
Is there another way?
User avatar
visualtech
VIP - Donator
VIP - Donator
Posts: 265
Joined: Sat Nov 19, 2011 2:19 pm

Re: Guessing game with loops
visualtech
Image
5 posts Page 1 of 1
Return to “Misc”