Skip to content
Advertisement

Level calculation

somehow I can’t think of a solution to a problem that is actually quite easy. It’s about a level system where I need a kind of formula that is very simple but I can’t think of it.

I calculate the level from the experience points and there you need e.g. for level 1 1.000 EXP, for level 2 3.000 EXP, level 3 6.000 EXP and so on.

Hopefully it is understandable.

The only problem is that it doesn’t go up consistently. So for level 2, for example, the 3,000 EXP result from the fact that you already have 1,000 for level 1 and then 2,000 EXP are added (level 2). And at level 3 it is 6,000 because of the 1,000 from level 1, the 2,000 from level 2 and then another 3,000 for level 3.

If you take it further, you need 5,000 EXP for level 5, for example, just to get from level 4 to 5, and before that you still have the other EXP of the other levels. This means that you need a total of 15,000 EXP for level 5.

Here is a list with the levels and the EXP required for them:

Level 2 -> 3.000
Level 3 -> 6.000
Level 4 -> 10.000
Level 5 -> 15.000
Level 6 -> 21.000
...
Level 10 -> 55.000

Advertisement

Answer

int xp=500;

//list of xp land marks to level up[adjust them to your needs]
int[] checkPoints=
{
  0,2000,3000,6000
 ,10000,15000,21000,
 ,25000,30000,35000
 ,40000,45000,50000
 ,55000
};

int level=0;
for(int i=0;i<checkPoints.length-1;i++)
{
 /*
  if xp lies with this range then level=i
  example xp=11000 lies between 10000[i=4] && 15000[i=5]
  so level=4
 */
 if(xp>=checkPoints[i] && xp<=checkPoints[i+1])
 {
   level=i;
 }
}

System.out.println("You are level="+level);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement