Results 1 to 4 of 4

Thread: Arrays

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Arrays

    For example, my XP is 878,
    and I have:
    Java Code:
    int levelxp[]={0, 0, 21, 78, 57, 248, 486, 1084, 2147};
    My level should be 8 because it is between 486 and 1084. How can I print this out?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by PhQ View Post
    For example, my XP is 878,
    and I have:
    Java Code:
    int levelxp[]={0, 0, 21, 78, 57, 248, 486, 1084, 2147};
    My level should be 8 because it is between 486 and 1084. How can I print this out?
    Check for a value i w.r.t. levelxp[i] <= XP <= levelxp[i+1]

    kind regards,

    Jos

  3. #3
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Java Code:
            int exp = 878;
            int levelxp[] = {0, 0, 21, 78, 57, 248, 486, 1084, 2147};
            int level = 0;
            for (int i = 0 ; i < levelxp.length ; i++)
            {
                if (levelxp [i] > exp)
                {
                    level = i;
                    break;
                }
            }
            System.out.println (level);
    something like this should work

    edit: Oh, are they not in order?
    Last edited by Cruncher; 04-18-2010 at 09:09 PM.

  4. #4
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Cruncher View Post
    Java Code:
            int exp = 878;
            int levelxp[] = {0, 0, 21, 78, 57, 248, 486, 1084, 2147};
            int level = 0;
            for (int i = 0 ; i < levelxp.length ; i++)
            {
                if (levelxp [i] > exp)
                {
                    level = i;
                    break;
                }
            }
            System.out.println (level);
    something like this should work

    edit: Oh, are they not in order?
    Thanks, that's what I was looking for.

Similar Threads

  1. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  2. Arrays
    By hypes057 in forum New To Java
    Replies: 13
    Last Post: 09-04-2009, 11:40 AM
  3. A little help with arrays..
    By zeppelin in forum New To Java
    Replies: 8
    Last Post: 01-05-2009, 01:33 PM
  4. Help on Arrays...
    By cuellar14 in forum New To Java
    Replies: 4
    Last Post: 07-25-2008, 09:16 PM
  5. need help with arrays
    By Jman in forum New To Java
    Replies: 17
    Last Post: 07-21-2008, 03:34 AM

Posting Permissions

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