Jess
July 17th, 2013, 07:11 PM
I'm nearly done completing this assignment; just need to sort out an error
/* 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";
}
}
}
I get an error on userAnswer in "int count = 1; userAnswer != computerNumber", saying it cannot find the symbol. I need the looping to end when the user guesses the correct number. What needs to be fixed?
/* 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";
}
}
}
I get an error on userAnswer in "int count = 1; userAnswer != computerNumber", saying it cannot find the symbol. I need the looping to end when the user guesses the correct number. What needs to be fixed?