Create a program that has the computer generate a random number between 1 and 100. The user is then asked to guess the number. If the user is wrong, they are informed whether the answer is too high or too low. The user is then allowed to guess again. This continues until the user guesses the number correctly. You must also include an introduction screen with instructions.
Hey
Hey
So I have some code, and i will post it here
kk
import java.util.*; class NumberGame { public static void main(String[] args) { int userValue; int randomNumber = 1; int (100 * Math.random(); } System.out.println("Welcome to the Random Number Guessing Game! A number between 1 and 100 will be generated at random. You are to enter a value within this range to guess the value. If you are wrong, you will be informed whether you are too high or too low. Keep guessing until you guess correctly."); for (randomNumber = 1; userValue <=100 && userValue >= 1; userValue ++))) System.out.println("Please enter a number between 1 and 100."); if (userValue == randomNumber) System.out.println("You have guessed correctly! Congratulations!!!"); else if (userValue > randomNumber System.out.println("Your guess is too high. Please try again."); else if (userValue < randomNumber System.out.println("Your guess is too low. Please try again."); else System.out.printIn("Sorry, please enter a valid number between 1 and 100.");
So, I am wondering whether I initialized everything properly.
Hmm, cool. I've never see the Math class's random method. I'm used to using the Random class.
I'm using loops for this
I can teach you how to make any random number- random between 0 and 1 (decimal) or random between +-2 billion or +- 9 quintillion, or a random number in a set range if you care for it.
ya, a loop which ends when you enter the right value sounds good.
First- that block of text from your first System.out.println(""); is long horizontally, add \n through out the text, each \n will enter it to be in a new line.
so it becomes a paragraph
also- do you intend for you if else if statements to be a part of the for loop? it would need curly bracuts so that it all loops for (code) { statemtne; statement; }
In the meantime I'll learn the Math's random method.
ok thanks !:D
ok. I've got it.
first off, in you're Math.random initialization, you don't have it's value being returned to any variable... you need something like <data type> <variable> = some version of Math.random()
Keep in mind that Math.random, like most of the methods under the Math class (.pow .sqrt) return doubles, so you'll need to either set the value it returns to a double, or cast it as an int, and then return it as an int.. though it would always return zero, because Math.random(); returns a random 15 digit decimal between 0 and 1; so you can counteract that by multiplying Math.random() to 100.
or multiplying it to however many of the 15 decimal places you want left of the decimal point.
I'm assuming you understand these terms though- so if it sounds like jibberish let me know :p
I can tell you about the different data types, and why for some of them you need to do a cast operator, like (int), and why sometimes it changes the data value automatically.
Be back in 10;
ok
Want me to teach ya?
@sciencewiz3000
Hey, that would be great if you could :)
allright, so Math.random(); returns a random decimal number between 0 and 1- how would you use the Math.random() to get a random number between 1 and 100?
So I want to get an integer value only so that's why I've defined the value as int
good- you're missing a closing parenthasis when you call it
oh ok thanks :)
also, when you want to make it into an int - or anything for that matter, you need parenthesis around around it:: Like so (int)Math.random(); instead of int Math.random();
great! That's what I was missing :P
another thing too.. now that you're (int)(Math.round() * 100); is written, how do you plan on saving that random value?
good point, do you think I should be using an assignment statement?
assignment statement? probably - how do you think it should look?
like maybe randomNumber = new randomNumber;
or should I initialize it to zero?
randomNumber looks good- now you have to make it equal to the random number- like randomNumber = (int)(Math.round() *100);
this will assign a random number of 0-99 to randomNumber
Ok and should I put this at the beginning of the program under the variable declarations?
ya, near the top is good- you can replace that statement with the hanging int (100 * Math.random(); line that you have
in the code you posted
ok let me try that
the Math.random() method will make a decimal between 0 and .99999, after you multiply by 100, it will always have an integer range of 0-99; I see in your post you want 1-100
gtg in 5
ok thanks for all of your help, looks like it is working with that. Thanks :)
do you know how to get user input?
yw sciencewiz. are you really gonna be allright?
you have a closing bracket near the top that shouldn't be there, extra closing parenthesis on the for loop, no means to get user input, missing brackets to outline the body of the for loop, and the for loop exits when you enter a number outside the 1-100 range, instead of getting the right answer.
gl sw.
Just a little FYI: To make code more readable, use a code paste site like dpaste or pastbin, OR use the code highligh system here. The code highlight here is triggered with ``` above and below the code block. That is the accent that also has ~ on it. Without: import java.util.*; class NumberGame { public static void main(String[] args) { int userValue; int randomNumber = 1; int (100 * Math.random(); With: ``` import java.util.*; class NumberGame { public static void main(String[] args) { int userValue; int randomNumber = 1; int (100 * Math.random(); ```
Well
@blahblah212121 The goal of the site is not to do work for people but to help them do their work. So if they have trouble, you point out where the issue is.
Join our real-time social learning platform and learn together with your friends!