Hey, I'm trying to code a guessing game that asks the user to enter a number in the range of 1 to 100 and tells them whether they are "too high" or "too low." The user keeps guessing until they get it right. When the answer is guessed, the get a "Congratulations....etc". I used a loop to ask the user to keep entering a value, but how do I include the too high too low part in the loop?
public class Random { public static void main (String [] args){ int randomNumber = (int) Math.random()*100; System.out.println("Welcome to the Random guessing game. Enter a number between 1 and 100."); int userValue = In.getInt(); do { System.out.println("Enter a number in the range of 1 to 100."); userValue = In.getInt(); } while (randomNumber !=userValue); if (userValue > randomNumber) { System.out.println("Your guess is too high."); } else if (userValue < randomNumber) { System.out.println("Your guess is too low."); } else if (userValue == randomNumber) { System.out.println("Congratulations! You guessed the number correctly!"); } } }
my code so far
When I run this, it just keeps printing "Enter a number" and then when you enter 0 it says the guessed correct number part.
How do you use this In.getInt() method? I've never seen it before
I'm guessing there are In.getDouble() | In.getFloat() | In.getShort() | In.getLine() methods as well
Yes, it just asks for user input.
int num = In.getInt(); System.out.println(num); what's wrong with this code? doesn't compile
It does compile and it works, but when I run the program, it keeps asking for the user to enter the value but doesn't return whether it is too high or low within the loop
I was not talking about your code- I meant the 2 lines of code that I just posted in my last post.. trying to figure out why In.getInt(); method in those 2 lines isn't compiling for me
then I'll help you after I figure this thing out
ok np,
do you know why In.getInt isn't working for me? is there an import statement involved?
how are you inputting it, could you copy what you are trying to compile and paste it here?
sure, thanks: //inside main method int num = In.getInt(); System.out.println(num); //inside main method
oh, your int num = In.getInt(); has to come after your print statement.
the print statement is read first and then adding in that line allows the user to actually enter input.
eh like this? //start int num = 0; System.out.println("Enter a number and watch it print:"); num = In.getInt(); System.out.println(num); //end
also, if you have assigned a value to num, you don't really need that part. I mostly just use it for strings.
ya, I know, I just wanna learn how it functions first.
ya
looks good
doesn't work for me lol :p "Error: unknown compilation problem; In cannon be resolved"
weird, so you assigned int num to 0, so then shouldn't it still work the way you did it or would it have problems with the value already assigned + the user input? Don't know, looks right lol :P
@blahblah212121 my question is at the top.
I think @sciencewiz3000 is using a 3rd party library, they are often provided in java courses to simplify IO for beginners. Do you mind pasting the entire file here, including imports? Also, when posting code on OS, dont forget to add 3 ` before and after your code to add markup. int num = In.getInt(); System.out.println(num); becomes ``` int num = In.getInt(); System.out.println(num); ```
3 ' System.out.println("Hello os, this is my first markup"); 3 '
Yes! I am using dr java lol I didn't do any imports on this one but i think you could use java.util.random* or something like that, but the code above is the entire code that I have used.
lol I look like a derp there. Anyway, thanks lyrae. Now it makes sense. I've yet to learn about libraries.
Okay i have a few minutes, I may suggest throwing this code into blue jay, to see the immediate compilation errors, as for actually helping you, I dont have enough time today, cause halloween and things, so if nobody helps you tonight I will be willing to give a full walkthrough on skype or something other than this website cause I have connection issues alot here
sciencewiz, remember that Math.random() will output a random decimal between 0 and 1, though your code as it is now will always result in variable randomNumber to equal 0. This is because before Math.random() is multiplied by 100, it is first cast as an int with your (int) cast operator. In order to get the random number 0-99, you must multiply Math.random() by 100 first, then cast it as an int.. you can do this by adding parenthesis around Math.random()*100.
using (int) on anything will drop any decimals if there are any, so naturally the random decimal between 0 and 1 will become 0.
Yes, and you have to move your checks if the number's correct/low/high inside the do-while loop.
Or rather, you only move checks for low/high inside do-while. When you're below the loop you already know that the user made a correct guess, so all you have to do is print the congratulations.
ok thanks :) let me try that.
Like this??? This doesn't work when I try to compile it. do System.out.println("Enter a number in the range of 1 to 100."); userValue = In.getInt(); if (userValue > randomNumber) { System.out.println("Your guess is too high."); } else if (userValue < randomNumber) { System.out.println("Your guess is too low."); } else if (userValue == randomNumber) { } while (randomNumber !=userValue); System.out.println("Congratulations! You guessed the number correctly!"); }
that empty else if(userValue == randomNumber) { } might be causing it
oh, your do while loop is missing a closing bracket
and a beginning bracket come to think of it..
It's a do-while. There are no brackets after the while-statement. To me it looks like you're missing a opening bracket after do.
Like this ``` do { System.out.println("Enter a number in the range of 1 to 100."); userValue = In.getInt(); if (userValue > randomNumber) { System.out.println("Your guess is too high."); } else if (userValue < randomNumber) { System.out.println("Your guess is too low."); } } while (randomNumber != userValue); System.out.println("Congratulations! You guessed the number correctly!"); ```
``` System.out.println("testing"); ```
Oh Ok, that works!! Thanks! :D
Sweet! Yw :) Don't forget to close the question if/when everything's working properly!
and @woodrow73 , you were saying that I needed to change the Math.random to make it so that the number is 1 to 100 instead of always 0, so I changed it to int randomNumber = (int) (Math.random()*100); but how would I fix it so that the range is 1 -100 ? Also, @Lyrae for some reason it does not return too high or too low for the first value that I enter when I run the program.
well currently, the range of Math.random()*100 is 0-99 because the smallest decimal is .00001ish and the largest is .9999ish, and multiplying by 100 will move the 2 digits closest to the decimal to the left side. so now that the range is (0-99), what would you change so that the range is (1-100) ? the answer isn't in any difficult syntax, just plain ol' math
so it would be Math.random()* 99 + 1 ?
na, I suppose that works 99.9% of the time, but it's easier to multiply that random decimal by 100, so the range is definitively 0-99, then adding one afterwards
ok so Math.random()* 100 + 1, awesome! Thanks :)
yw :) multiplying it by 99 will just slightly increase the odds that the random decimal produced will be 1 less than by multiplying it by 100 ie: .9900*99 = 98
got it! thanks again, and lol are you a university student or a programmer because you really know your stuff! :P
lol thanks. neither at the moment, just a programming hobbyist, with some goals.
I'm programing at university + I work part time @ Ericsson with Java development.
cool! I'm guessing the job there is mostly a learning experience more so than the money?
Yeah, exactly! The pay isn't that great, but I've learned more there than @ uni :P
Anyway did you get that loop to work? It run perfectly for me :P And I couldn't find any error with the logic either. http://ideone.com/EQipeG
nice. Good luck to yourself. + code looks good to me also.
Yep, I worked it out, I just had to get rid of the In.getInt part under the first print statement in my original code, but now everything is working just fine!
Thanks again to both of you.
Sure thing @sc
Yw :) Happy halloween!
Yes, enjoy the rest of your Halloween! :)
@sciencewiz3000 pm me if you ever need more help and close this
Join our real-time social learning platform and learn together with your friends!