在Java中将枚举值设置为随机值
问题描述:
import java.util.*;
public enum Skills {
Random r= new Random();
//trying to use nextInt method in some case
SKILLS(r.nextInt(125)+25),
VEHICLE(r.nextInt(125)+25),
ACCURACY(),WEAPONS(23),REFLEX(),
STRATEGY(),CHARISMA(),
HACKING(r.nextInt(125)+25),
SPEED(r.nextInt(125)+25),
STEALTH(r.nextInt(125)+25);
//end of skills
private int value;
private Skills(int value){
this.value=value;
}
public int getValue() {
return value;
}
}
我无法将我的枚举值设置为随机值。之后我会将这些技能提供给我的游戏角色。我也无法使用nextInt方法。这是为什么 ?如何解决问题并正确使用此枚举?在Java中将枚举值设置为随机值
答
为此,使用enum
没有任何意义。请记住,enum
常量是单身人士:所有角色将共享每个技能对象的相同单个副本,因此所有角色都具有相同的技能编号。这可能不是你想要的。
它会更有意义,只使用一类区域,而不是:
public class Skills {
private int vehicleSkill;
private int hackingSkill;
// etc.
public Skills(Random r) {
this.vehicleSkill = r.nextInt(125)+25;
this.hackingSkill = r.nextInt(125)+25;
// etc.
}
public int getVehicleSkill() {
return vehicleSkill;
}
public int getHackingSkill() {
return hackingSkill;
}
// etc.
}
这样,你可以为每个角色单独Skills
对象。
+0
谢谢,帮助了很多,在此之后,我将这些附加为人类属性的值。像这样:this.hacking = Skills.hackigSkill; ?? – Viktor
阅读有关枚举并查看示例,然后回来问这个问题。 – Tarik