我在弄清楚如何制作我的水平系统时遇到了麻烦。当某人越过需要xP线时,他们应该升级,该方法应打印此信息以及一些操作员功能。仅当值a穿过值b时。或者在这种情况下。如果值a不再是值a,而是变为b。但是只有当它改变时。
// Leveling system
String[] levels = {"Bitch", "Trainee", "Rookie", "Supervisor", "Pro", "Killer Machine", "Infamous", "Legendary", "Badass", "Badass MOFO"};
Integer[] xPs = {250, 750, 1500, 3000, 5000, 8000, 12000, 18000, 24000, 36000};
Integer[] maxHealthArray = {155, 190, 225, 260, 295, 330, 365, 400, 435, 470};
if(xP < xPs[0]) {
System.out.println("\nYour xP is: " + xP);
System.out.println("You have reached a rank of: " + levels[0]);
maxHealth += maxHealthArray[0];
}
else if(xP < xPs[1]) {
System.out.println("\nYour xP is: " + xP);
System.out.println("You have reached a rank of: " + levels[1]);
maxHealth += maxHealthArray[1];
}
else if(xP < xPs[2]) {
System.out.println("\nYour xP is: " + xP);
System.out.println("You have reached a rank of: " + levels[2]);
maxHealth += maxHealthArray[2];
}
else if(xP < xPs[3]) {
System.out.println("\nYour xP is: " + xP);
System.out.println("You have reached a rank of: " + levels[3]);
maxHealth += maxHealthArray[3];
}
else if(xP < xPs[4]) {
System.out.println("\nYour xP is: " + xP);
System.out.println("You have reached a rank of: " + levels[4]);
maxHealth += maxHealthArray[4];
}
else if(xP < xPs[5]) {
System.out.println("\nYour xP is: " + xP);
System.out.println("You have reached a rank of: " + levels[5]);
maxHealth += maxHealthArray[5];
}
else if(xP < xPs[6]) {
System.out.println("\nYour xP is: " + xP);
System.out.println("You have reached a rank of: " + levels[6]);
maxHealth += maxHealthArray[6];
}
else if(xP < xPs[7]) {
System.out.println("\nYour xP is: " + xP);
System.out.println("You have reached a rank of: " + levels[7]);
maxHealth += maxHealthArray[7];
}
else if(xP < xPs[8]) {
System.out.println("\nYour xP is: " + xP);
System.out.println("You have reached a rank of: " + levels[8]);
maxHealth += maxHealthArray[8];
}
else if(xP < xPs[9]) {
System.out.println("\nYour xP is: " + xP);
System.out.println("You have reached a rank of: " + levels[9]);
maxHealth += maxHealthArray[9];
答案 0 :(得分:0)
为什么不设置枚举级别?
public enum Levels {
BITCH("bitch", 250, 155);
TRAINEE("trainer", 750, 190);
private final String level;
private final int xP;
private final int maxHealth;
private Levels(String level, int xP, int maxHealth) {
this.level = level;
this.xP = xP;
this.maxHealth = maxHealth;
}
public String getLevel(){
return this.level;
}
public int getXP(){
return this.xP;
}
public int getMaxHealth(){
return this.maxHealth;
}
}
并使用EnumSet遍历:
EnumSet.allOf(Levels.class).forEach(level -> {
if (xP < level.getXP()){
System.out.println("\nYour xP is: " + xP);
System.out.println("You have reached a rank of: " + level.getLevel());
maxHealth += level.getMaxHealth();
}
}
);
请注意,上面的代码并未经过实际测试,但应该可以给您一个提示。
答案 1 :(得分:0)
我认为OP想要的东西只能在XP边界条件下触发一次。如果我们继续Enum
的想法:
enum Level {
Paladin(750, 190, null),
Peon(250, 155, Paladin);
private final int maxXp;
private final int healthBuff;
private final Level next;
Level(int maxXp, int healthBuff, Level next) {
this.healthBuff = healthBuff;
this.maxXp = maxXp;
this.next = next; // Questionable means to know "next" Level but is at least final
}
// returns null if not applicable
Level promotionLevel(int xp) {
return xp > maxXp ? next : null;
}
}
public void checkPromotion() {
Level next;
// while() handles multiple promotions, might not be relevant
while ((next = this.level.promotionLevel(this.xp)) != null) {
promoteTo(next);
}
}