Results 1 to 4 of 4
Thread: Guessing Game with for loop
- 11-21-2010, 11:43 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
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!";
}
}
}
- 11-22-2010, 12:06 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
You answered your own question.
The exit condition of a for loop can be anything, doesn't have to be related to the counter at all:userAnswer == computerNumber
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.
- 11-22-2010, 12:27 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
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.
- 11-22-2010, 12:55 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
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.Java Code:for (int count = 1; userAnswer != computerAnswer; count ++)
Ever seen a dog chase its tail? Now that's an infinite loop.
Similar Threads
-
Need help in Guessing Game
By rose in forum Java GamingReplies: 4Last Post: 10-27-2010, 10:43 PM -
Guessing Game
By rose in forum Java GamingReplies: 4Last Post: 10-27-2010, 08:00 PM -
guessing game help
By yasmin k in forum AWT / SwingReplies: 4Last Post: 10-31-2009, 05:37 PM -
guessing game using GUI
By yasmin k in forum New To JavaReplies: 1Last Post: 10-26-2009, 12:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks