Android - Optimising Multiple If-statements
Solution 1:
Perhaps store your level experience requirements in an array and use a loop.
int[] levels = newint[] { -1, Level1, Level2, Level3 };
int level;
for (level = 0; level < levels.length && userXp < levels[level]; level++) { }
int minLevel = levels[level];
int maxLevel = levels[level + 1];
userLevelText.setText(Integer.toString(minLevel));
Solution 2:
Why not do something more generic?...
if (User_XP > Minimum_Level) {
User_level.setText(Integer.toString(++Minimum_Level));
}
Of course if need the min/max instead of just doing a calculation..
if (User_XP > Minimum_Level) {
User_level.setText(Integer.toString(++Minimum_Level));
Maximum_Level = Minimum_Level + 1;
}
Then you could add in bounds checking before hand if you want to limit the absolute number if levels like if(Minimum_Level == 10) return; //Too much awesome!
Solution 3:
It seems like User_level.setText(String.valueOf(User_XP-1);Minimum_Level = User_level; Maximum_level = User_XP;
or something in that lines will do the job. The point is there seems to be some logic there, you should be able to eliminate the if statement all together.
Solution 4:
In such cases I prefer making a static [not the java key word] propertiesfile/map/table and store the details in the properties,map or table. And then at run time read from the map and set the appropriate values or do things. This help is expansion without coding by simply adding values to
Post a Comment for "Android - Optimising Multiple If-statements"