Results 1 to 3 of 3

Thread: Some help

  1. #1
    Join Date
    Mar 2008
    Location
    New Jersey
    Posts
    1,673
    Mentioned
    1 Post(s)
    Quoted
    9 Post(s)

    Default Some help

    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 . 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 , 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....
    Last edited by Baked0420; 09-18-2009 at 08:58 AM.

  2. #2
    Join Date
    Mar 2007
    Posts
    3,042
    Mentioned
    1 Post(s)
    Quoted
    14 Post(s)

    Default

    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.
    :-)

  3. #3
    Join Date
    Mar 2008
    Location
    New Jersey
    Posts
    1,673
    Mentioned
    1 Post(s)
    Quoted
    9 Post(s)

    Default

    I was in IRC earlier and was like, where's method, he should know, I was thinking where are you when I need you , 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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •