This is my code. It is fairly simple, I am just asking the user for a guess, and telling them if they are correct, high or low to the number. To continuously ask, I made a while loop.
Although, I am not sure why or how it works the way it does. I initialize the int as -1 for guess, and it seems to work. Why wont this work without initializing it at all, and why a negative number to begin with. Doing this just seems to be a bit out of place. I am guessing this isn't common practice?
Thanks again! :)
import java.util.Scanner;
public class numGuess
{
public static void main(String[] args)
{
//generate random number between 1 and 100
int number = (int)(Math.random() * 101);
Scanner input = new Scanner(System.in);
System.out.println("Guess a number between 1 and 100");
int guess = -1;
while (guess != number)
{
System.out.print("\nEnter your guess: ");
guess = input.nextInt();
if (guess == number)
System.out.println("you are correct, the number is " + number);
else if (guess > number)
System.out.println("your guess is too high");
else
System.out.println("your guess is too low");
}
}
}