i\'m learning some Java and decided to do something simple but it keeps returning the wrong value... i don\'t know why i\'m sure there something that my untrained eye isn\'t pic
if (userxp >=level1xp || userxp <level3xp){
That should be &&
not ||
.
And my minimal suggestion would be to completely drop the first condition.
Since you are in the else branch, you have already checked that the XP is not lower than the previous level requirement.
if (userxp <level2xp){
userlevel=1;
}else if ( userxp <level3xp){
userlevel=level2;
System.out.println(userlevel);
}else if ( userxp <level4xp){
userlevel=level3;
System.out.println(userlevel);
}else if (userxp <level5xp){
userlevel=level4;
}else if (userxp == level5xp){
userlevel=level5;
}else{
System.out.println("You are a cheat");
}
You condition is wrong use &&
instead of ||
. Because in your condition whenever the first part of the condition is true the condition becomes true.
import java.util.Scanner;
import java.lang.*;
public class HelloWorld{
public static void main(String args[]){
int userlevel = 0;
int userxp=12;
int level1xp=0, level2xp=1, level3xp=10, level4xp=15, level5xp=25;
int level1=1, level2=2, level3=3, level4=4, level5=5;
if (userxp <=level1xp){
userlevel=1;
}else if (userxp >=level1xp && userxp <level3xp){
userlevel=level2;
System.out.println(userlevel);
}else if (userxp >= level3xp && userxp <level4xp){
userlevel=level3;
System.out.println(userlevel);
}else if (userxp >=level3xp && userxp <level5xp){
userlevel=level4;
}else if (userxp == level5xp){
userlevel=level5;
}else{
System.out.println("You are a cheat");
}
}
}
if i would be you, and if you have finite number of levels, i would go with enums and i would do something like that
public class HelloWorld {
private static enum Level {
LEVEL_1(0), LEVEL_2(1), LEVEL_3(10), LEVEL_4(15), LEVEL_5(25);
int expRequired;
Level(int expRequired) {
this.expRequired = expRequired;
}
static Level getLevel(int exp) {
Level level = LEVEL_1;
for (Level l : Level.values()) {
if (l.expRequired > exp) {
break;
}
level = l;
}
return level;
}
}
static class User{
Level level = Level.LEVEL_1;
int exp = 12;
}
public static void main(String args[]) {
User user = new User();
int level1xp = 0, level2xp = 1, level3xp = 10, level4xp = 15, level5xp = 25;
int level1 = 1, level2 = 2, level3 = 3, level4 = 4, level5 = 5;
user.level = Level.getLevel(user.exp);
System.out.println(user.level );
}
}
I think you just need to change your || (or) to && (and). Otherwise, if your xp is 12 which is greater than level1xp (0), your program will set your level to level 2 immediately because of this if statement:
else if (userxp >= level1xp || userxp < level3xp)
An xp level of 12 will pass these requirements since 12 is greater than 0. Changing || or && will solve this problem I think. Let me know if you have any other problems after that.
The out put is 2 because it checks the first if statement which turns out to be false. Then it moves to the if else where it is true as userxp is in fact >=level1xp
so userlevel is set to 2 and printed. So as far as I can understand the codes objective, replace the ||(OR)
with &&(AND)
You should use &&
(AND) instead of ||
(OR) in your conditions.
Try else if (userxp >=level1xp && userxp <level3xp)
using in all of your else if statements.
In your code, the execution stops after the first else statement, which is true.
if (userxp <=level1xp){
userlevel=1;
}else if (userxp <level3xp){
userlevel=level2;
System.out.println(userlevel);
}else if (userxp <level4xp){
userlevel=level3;
System.out.println(userlevel);
}else if (userxp <level5xp){
userlevel=level4;
}else if (userxp == level5xp){
userlevel=level5;
}else{
System.out.println("You are a cheat");
}
EDIT: as pointed out in the owther answers, you do not need the first condition for the lower bound of the level since it is already satisfied by the previous else statement.