|
GuessingGame - help with code
Hi all,
New to this forum, and new to Java. I'm trying to create a guessing game where the user is required to guess a number between 1 to 100. The game provides feedback to the user if the guess is higher or lower. When the user does guess the number, they are given a message with the total number of attempts.
When I run this code, I get an error on the last line that 'guess cannot be resolved'. Any help would be appreciated.
import java.util.Scanner;
public class GuessingGame2
{
public static void main (String[] args)
{
int tries = 0;
double randomNum = (int)(Math.random() * 100 + 1);
int num = (int)(randomNum + 1);
System.out.print(num);
do
{
System.out.print("Please enter a number between 1 and 100: ");
Scanner numGuessString = new Scanner(System.in);
int guess = numGuessString.nextInt();
tries++;
if(guess>num)
{
System.out.println("Too high. Try again.");
System.out.println();
}
if(guess<num)
{
System.out.println("Too low. Try again.");
System.out.println();
}
if(guess==num)
{
System.out.println("Correct! It took you " + tries + " tries to guess the number!");
}
} while (guess!=num) ;
}
}
|