I am recieving a syntax error and I can't figure out why. I am in an intro to programming course so my knowledge is very limited. This is a project I have to do and the code I have so far. I just want to test it to see if I am on the right path, but I cant. Any help would be GREATLY appreciate so I can at least see what my next issue is going to be haha.
import java.util.*;
public class project2 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.println("Welcome, I am thinking of a number between 1-100. Take a guess!");
System.out.print("Please enter your number: ");
int guess = in.nextInt();
Random r = new Random(101);
int counter = 0;
int totalcount = 6;
While (counter != totalcount) { // being told to insert ";" and I have no clue why
if ( guess < r) {
System.out.print("You guessed " + guess);
System.out.println("Too Low");
} else if ( guess > r) {
System.out.print("You guessed " + guess);
System.out.println("Too High");
} else ( guess = r ) {
System.out.print("You guessed " + guess);
System.out.println("YOU'RE PSYCHIC!");
}
counter++;
}
}
}
You have incorrectly capitalised While. It should be while (all lower-case).
See the documentation for while.
However, there are a number of other issues with your code.
Firstly, if you have tested that a number is not greater than or less than it must, by definition be equal. Therefore, you can change
else ( guess = r ) {
to
else
This is fortunate for you as guess = r will not test for equality. Instead it will assign the value of r to guess. Also, because else (condition) is a syntax error.
Secondly You are trying to compare an int to a Random in this line:
if ( guess < r) // r is of type Random, guess is of type Int.
What you should actually do is use your Random to obtain an int, e.g.:
Random rnd = new Random();
int r = rnd.nextInt();
if (guess < r) // This is now valid.
Thirdly You have seeded your random number generator with 101. This means that it will always return the same sequence of numbers. This may be what you want but, if it is not, you can just use the paramaterless constructor to seed it with the current time in milliseconds
Random rnd = new Random();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With