Hi! I've been given a guessing game to make on some already written codes by my advisor but I'm really stuck.
This what I was told to do and couldn't solve!
--In this guessing game, the user should be prompted to type in a number as long as long as the user’s answer doesn’t match the number generated by the computer.
--To adjust this program further, adjust and add lines to “count” the number of guesses that a user has taken. Create an integer variable called count to keep track of how many
guesses a user has taken. This can be done in the main method.
--Each time a user guesses, we will need to add 1 to the count variable.
-- Display the number of guesses each time you tell the user whether their guess is correct, incorrect, or invalid. This should be added to the JOptionPane message box that is
displayed in the main method.
here is my code: it shows error at line 18! Please help
/* This is a Java program to allow a user to guess the computer 1 to 100. * Adnan Alvee * 10/18/12 * JDK 1.6 */ import javax.swing.JOptionPane; import java.util.Scanner; public class GuessingGame { public static void main(String[] args) { int computerNumber = (int) (Math.random()*100 +1); //generates a random number from 1 to 100. System.out.println("The correct guess would be " + computerNumber); int userAnswer = Integer.parseInt(response); <-------------------------------------------------This is line 18 do { String response = JOptionPane.showInputDialog(null, "Enter a guess between 1 and 100", "Guessing Game", 3); } while (userAnswer != computerNumber); JOptionPane.showMessageDialog(null, "Your guess is " + determineGuess(userAnswer, computerNumber) + "\nTry number: " ); } public static String determineGuess(int userAnswer, int computerNumber) { if (userAnswer <=0 || userAnswer>=100) { return "invalid"; } else if (userAnswer == computerNumber) { return "correct"; } else if (userAnswer < computerNumber) { return "Your guess is too low"; } else if (userAnswer > computerNumber) { return "Your guess is too high"; } else { return "incorrect"; } } //ends determineGuess method } //ends program