PDA

View Full Version : Some help



Baked0420
09-18-2009, 08:46 AM
Doing some java in AP Computer Science. I am a little behind, like some people finished a small little project we started yesterday, but that day I was finishing a different one, well anyway, I decided to do it at home to catch up and just pastebin it, but I got no teacher at my house :p. I might have a few questions after I get this one answered, but for now, my teacher said we need to do casting to type so that we can convert a float or double, I think float, to an integer, I forget what he used, anyone have an idea? And the second question, we need to get 6 random numbers, 1-49, I know it's something like (49)+1, because 49 does 0-48, so + 1 makes it's 1-49, but I'm missing the method :p, in Pascal it'd be random(49)+1, what would that be in Java? Oh yea, I think it returns a decimal, the random number 0-48, and need to multiply it, but I just need to know the method name and I'll figure out the coding. Any help is greatly appreciated.


EDIT: I figured out the second part, it was:

int q = (Math.random()*48) + 1;

now I just need to convert it into a integer with something before the (Math.random....

Method
09-18-2009, 10:03 AM
You want to type cast the result to an int? If so,

int random = (int) (Math.random() * 48) + 1;

or something like that should do, depending on what you want.

If you have any more questions, feel free to message me on MSN or IRC and I'll help you out.

Baked0420
09-18-2009, 10:47 AM
I was in IRC earlier and was like, where's method, he should know, I was thinking where are you when I need you :p, but I figured it out.

I wanted to type it out as an int, but Math.round(); the variable in round must be a long, so I just switched it from an int to a long, worked much easier then trying to figure out how to make:

int x = (Math.random() * 48) + 1;
System.out.println(x); work

I made it:

long x= Math.round(Math.random() * 48) + 1;
System.out.println(x);


I'll try what you said though, to see if I can make it an int instead of a long.

I tried it and it worked, I'd rather it be an integer, so thanks very much Method :)