Need help converting random number to a int
Just what the title says heres my code Code:
import java.io.*;
import java.util.*;
public class prog210c {
public static void main(String args[])
{
Random rndm = new Random();
double rm = (Random) rndm;
int count = 0;
int x = 0;
for(int j = 0; j < 1; j++)
{
rndm.nextInt(100);
if(x != rndm)
{
System.out.println("Sorry try again...");
count++;
}
}
}
}
Re: Need help converting random number to a int
Does the code compile? If not, and you can't understand the compiler's messages copy and post them. Someone is sure to be able to explain what they mean.
---
There are quite a few variables in that code. Make sure you have a clear idea what you expect each one to be (and how they act together to do whatever you want the program to do - you don't say what that is). In fact it is a good idea to choose as variables something that is descriptive. j is a bit of an exception because it is clear what it is doing. But count for example: what is it supposed to be a count of? Decide, then consider changing it to whateverCount.
Also follow Java coding conventions and change the name of the class to Prog210c. That's a small thing, but it's a habit worth getting in to.
Re: Need help converting random number to a int
Quote:
Originally Posted by
pbrockway2
Does the code compile? If not, and you can't understand the compiler's messages copy and post them. Someone is sure to be able to explain what they mean.
---
There are quite a few variables in that code. Make sure you have a clear idea what you expect each one to be (and how they act together to do whatever you want the program to do - you don't say what that is). In fact it is a good idea to choose as variables something that is descriptive. j is a bit of an exception because it is clear what it is doing. But count for example: what is it supposed to be a count of? Decide, then consider changing it to whateverCount.
Also follow Java coding conventions and change the name of the class to Prog210c. That's a small thing, but it's a habit worth getting in to.
No the code dosent compile lol thats the problem. The reason its not compiling is cause i cant make the if statment compairing a int to a random. Code:
Random rndm = new Random();
double rm = (Random) rndm;
here you can see that i am generating a random number and then i am trying to make a double variable the same as the random number generated but i cant figure out how to do it thats why i posted here.
Re: Need help converting random number to a int
Quote:
Originally Posted by
MrPosotive
Code:
Random rndm = new Random();
double rm = (Random) rndm;
here you can see that i am generating a random number and then i am trying to make a double variable the same as the random number generated but i cant figure out how to do it thats why i posted here.
No, you're not generating a random number at all; you're trying to convert a random number generator to a double. A generator isn't a number itself ...
kind regards,
Jos
Re: Need help converting random number to a int
Quote:
Originally Posted by
JosAH
No, you're not generating a random number at all; you're trying to convert a random number generator to a double. A generator isn't a number itself ...
kind regards,
Jos
Well thanks for letting me know what that code is doing. Now can you please help me get the number stored as a int or double.
Re: Need help converting random number to a int
Re: Need help converting random number to a int
Quote:
Originally Posted by
JBelg
I looked at it but its not much help. I needto find a way to store the random number as a int.
Re: Need help converting random number to a int
Acutally i figured it out. but now ive run into one last problem, whenever it goes into the if loop i need to be able to keep entering numbers so that the person keeps guessing but i can enter it once and then it wont let me enter it again for some reason even though it repeats the loop Code:
import java.io.*;
import java.util.*;
public class prog210c {
public static void main(String args[])
{
int a = (int) (Math.random()*20);
int count = 0;
int x = 0;
for(int j = 0; j < 1; j++)
{
System.out.println("Please guess a number ");
int c = 0;
if(c != a )
{
Scanner b = new Scanner(System.in);
c = b.nextInt();
System.out.println("Sorry try again...");
count++;
}
else
{
System.out.println ("Your correct! The number was " + a + " You guessed " + count + "times");
}
}
}
}
Re: Need help converting random number to a int
Quote:
Originally Posted by
MrPosotive
Well thanks for letting me know what that code is doing. Now can you please help me get the number stored as a int or double.
Your code isn't doing anything because it doesn't even get passed the compiler. Reading the API for the Random class and reading up on the basics of the Java language might do wonders ...
kind regards,
Jos
Re: Need help converting random number to a int
Quote:
Originally Posted by
JosAH
Your code isn't doing anything because it doesn't even get passed the compiler. Reading the API for the Random class and reading up on the basics of the Java language might do wonders ...
kind regards,
Jos
It was compiling without the one part that i was trying to convert the random generator to a int. Your really not much help for a mod are you?
Re: Need help converting random number to a int
Quote:
Your really not much help for a mod are you?
I think we can all do without the personal comments.
Jos, like everyone else here, moderator or not, offers their help generously. And that generosity ought to be appreciated.
For better help, apply yourself to tidying up your code, your grammar, and your descriptions of your problems. The last includes precisely specifying observed and intended program behaviour and actually posting compiler messages.
Quote:
even though it repeats the loop
The loop you posted in #8, whatever it is intended to do, will execute exactly once as you can see from the number of times it prints "Please guess a number ".
Re: Need help converting random number to a int
Quote:
Originally Posted by
pbrockway2
I think we can all do without the personal comments.
The loop you posted in #8, whatever it is intended to do, will execute exactly once as you can see from the number of times it prints "Please guess a number ".
Yea that loop was pointless it was apart of another thing i tried and it didnt worki forgot it was there. But the problem is still there The loop here Code:
if(c != a )
{
Scanner b = new Scanner(System.in);
c = b.nextInt();
System.out.println("Sorry try again...");
count++;
b.nextInt();
its still not looping correctly, i need to be able to input a number until the imputed number equals the random number that was generated but every time i run it this is the output " Please guess a number
1
Sorry try again...
2"
and then the program terminates.
Re: Need help converting random number to a int
That isn't a loop at all. Possibly a do-while loop could be used, or a while loop and break when you're done. (I have a thing against do-while, others seem cagey about breaking...)
Code:
Declare and set numberToBeGuessed
Print "Guess a number"
WHILE true (ie forever)
Declare and get guess
IF guess is numberToBeGuessed
break
ELSE
Print "Guess again"
END WHILE
The important thing is the algorithm - the steps. Decide on this before you code. A good algorithm should read like ordinary language: it's worth a few key presses to say numberToBeGuessed for a number to be guessed as opposed to 'a'.
Decide on whether the algorithm sketched out above would do what you want. Code it (even if it doesn't do everything you want) and deal with compiler messages. Note you aren't fighting the compiler - compiler messages do not mean "the code does not wrok", rather they are intended as helpful messages to assist you better express what you mean in Java. Test the sucessfully compiled code to ensure it really does do (if partially) what you intend.
Decide how you would extend it to count the guesses (what's a good name for a variable that counts the number of guesses? where should the counter be declared? what should its initial value be? where should it be incremented?). Again code, compile, test.
If you want to limit the number of guesses, decide where you would include that check. Code, compile, test. Perhaps at this point consider whether other constructs would more fluently express the desire to limit the number of guesses. For example a for loop: decide on an algorithm involving a for loop, code that, compile and test it.
Re: Need help converting random number to a int
Quote:
Originally Posted by
MrPosotive
It was compiling without the one part that i was trying to convert the random generator to a int.]
That doesn't surprise me one bit; you can ruin a perfectly fine program (it compiles and runs ok) by adding one single line that doesn't make sense at all; your '(double)Random' was such a line.
Quote:
Your really not much help for a mod are you?
I help people who want to be helped.
kind regards,
Jos
Re: Need help converting random number to a int
Quote:
Originally Posted by
JosAH
I help people who want to be helped.
I wanted help, you pretty much told me what i already knew, and being sarcastic telling me that Reading Java basics will do wonders was the most usless thing any one has told me on these forums so far. I needed help trying to figure out a problem and my last "Solution" didnt work therefor it wasnt compiling i thought i was just doing one little thing wrong.
Re: Need help converting random number to a int
Random is itself is not a number, it's an Object that is able to generate completely random numbers.
To generate a random number from such an Random object you need to ask it. So in your case you made a Code:
Random rndm = new Random();
this is good, next you need to call one if it's method which will return a number (int, double, long ...) .
If this doesn't help you, you'll have to go back and study the basics of Java.
Re: Need help converting random number to a int
Quote:
Originally Posted by
JBelg
Random is itself is not a number, it's an Object that is able to generate completely random numbers.
To generate a random number from such an Random object you need to ask it. So in your case you made a
Code:
Random rndm = new Random();
this is good, next you need to call one if it's method which will return a number (int, double, long ...)
.
If this doesn't help you, you'll have to go back and study the basics of Java.
Lol yea i figured that out with the help of some one above, thanks though!