Beginner, stuck on implementing while loop, compiles fine but still won't run
Ahoy there,
Thanks for taking the time to read this. I'm in a programming class and this is my first adventure into the subject.
I'm working on a code that is supposed to be a number guessing game.
Here's where I am at so far
Code Java:
import java.util.*;
import java.io.*;
public class number_guessing_game {
public static void main (String[] args) {
BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
double attempts = 0;
double random = 0;
double guess = 0;
String go = "";
Random generator = new Random ();
int answer = generator.nextInt(10) + 1;
while(go.equals("yes")) {
System.out.println("How many chances do you want? ");
try {
attempts = Double.parseDouble(input.readLine());
}
catch (IOException E) {
System.out.println(E);
}
System.out.println("What's the magic number? (1-10)? ");
try {
guess = Double.parseDouble(input.readLine());
}
catch (IOException E) {
System.out.println(E);
}
if (guess == answer) {
System.out.println("Congrats! You're guess is right");
}
else if (guess != answer) {
System.out.println("Sorry, that is not correct! Would you like to try again?");
try {
go = input.readLine();
}
catch (IOException E) {
System.out.println(E);
}
}
}
}
}
I exit nano, compile with no exceptions, but when I go to run it, I just get the next line:
student21@slisdl1:~/LS590/Labs/Lab4> javac number_guessing_game.java
student21@slisdl1:~/LS590/Labs/Lab4> java number_guessing_game
student21@slisdl1:~/LS590/Labs/Lab4>
Any ideas? I've gone over it a ton and still can't put my finger on the problem.
Thank you in advance for any suggestions[-O<
Re: Beginner, stuck on implementing while loop, compiles fine but still won't run
i'm running it but im not getting anything.
is the user supposed to input something?
Re: Beginner, stuck on implementing while loop, compiles fine but still won't run
Quote:
Originally Posted by
clydefrog
i'm running it but im not getting anything.
is the user supposed to input something?
Yes, sorry should have put that out there. They're supposed to say how many guesses they want and then attempt to guess the randomly generated number in that many tries.
If I take the while loop at it works fine.
Re: Beginner, stuck on implementing while loop, compiles fine but still won't run
when i run it nothing happens. but when i change the variable String go = ""; to String go = "yes"; it appears to be work as far as i know
so yeah...lol. was that the issue? because when you have it as String go = ""; then basically the while loop is never hit, which in turn never allows for a guess.
Re: Beginner, stuck on implementing while loop, compiles fine but still won't run
Holy cow, yep that did it.
I tried testing that earlier, but I didn't realize I had it as go = "yes " with a space so it never recognized my yes. /facepalm
Thanks so much!
Re: Beginner, stuck on implementing while loop, compiles fine but still won't run
haha no problem.
yeah, the simplest mistakes are the hardest to find. :)