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!";
}
}
}