Guessing game with loops
Questions/Tutorials for any programming language not covered in other sections.
5 posts
Page 1 of 1
Code: Select all
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./* 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 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?
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
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
That doesn't end the loop when the correct number is guessed (unless I misunderstood...?)
Is there another way?
Is there another way?
this might be of some help : http://www.java-made-easy.com/guessing-game.html
5 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023