begginer wondering why his guessing game won't work
heres the code:
Code :
Scanner sc = new Scanner(System.in);
Random rand = new Random();
boolean running = true;
String yesno;
String y = "y";
String n = "n";
boolean guessrunning = true;
int i = 0;
int low = 0;
int high = 0;
boolean highlow = true;
int guess = 0;
boolean guesscheck = true;
System.out.println("Would you like play? Y/N");
while (running == true) {
yesno = sc.next();
if (yesno.compareToIgnoreCase(y) == 0) {
while (highlow == true) {
System.out.println("please enter the highest number");
high = sc.nextInt();
System.out.println("please enter the lowest number");
low = sc.nextInt();
if (high - low <= 0) {
System.out.println("Please enter a high number which is higher than the low number");
} else {
highlow = false;
}
}
highlow = true;
System.out.println("Please enter your Guesses");
int randomnum = rand.nextInt(high - low) + low;
System.out.println((int) Math.sqrt(high - low));
for (i = 0; i < (int) Math.sqrt(high - low); i++) {
while (guesscheck = true) {
guess = sc.nextInt();
if (guess > high || guess < low) {
System.out.println("Please enter a number that is between your low and high numbers");
} else {
guesscheck = false;
if (guess == randomnum) {
System.out.println("congratulations!! you got it right!");
i = (int) Math.sqrt(high - low);
} else {
System.out.println("Wrong, try again");
}
guesscheck = false;
}
}
}
System.out.println("Play again? Y/N");
} else if (yesno.compareToIgnoreCase(n) == 0) {
running = false;
} else {
System.out.println("Please enter Y for yes or N for no");
}
}
it does have stuff before and after it, thats the core of it though
the problem is after you have entered high and low, you then have infinite guessses, despite my best attempts at making you only have the square root of high - low guesses
this is a problem that i am doing for school, thats why its crazy, but its no assessment task, so no holding back :)
thanks in advance :)
Re: begginer wondering why his guessing game won't work
These ones can drive you crazy, it took me a couple scans over your code to spot it.
Code :
while (guesscheck = true) {
You wanted a "==" in there :)
What this does is first assign guesscheck the value true, then check to see if that evaluates to true (which it does), so your loop never exits.
Re: begginer wondering why his guessing game won't work
god damn it....i was puzzling over that for ages...i though i had checked that....thanks for the response though, my brother had a look and he removed the while loop basically, and did a couplke of other things to get it to work...but thanks heaps, i would be wondering why it didn't work forever otherwise