【发布时间】:2020-06-29 14:46:38
【问题描述】:
我正在使用 Java 制作基于文本的游戏,我正在使用类和实例变量,并且我正在尝试创建一个方法,该方法根据实例变量 max_attack 的数字生成随机数。
public class badPlayer
{
String description;
int health;
int award;
int max_attack;
public badPlayer(String description, int health, int award, int attack){
this.description=description;
this.health = health;
this.award = award;
this.max_attack = attack;
}
Random rnd = new Random();
public int maxAttack(){
int rand_int1 = rnd.nextInt(max_attack);
return rand_int1;
}
}
public class troll extends badPlayer
{
troll(int attack){
super("troll", 100, 50, 100);
}
}
输出
巨魔1005089
我想要创建 1 到 100 之间的随机数的方法。
【问题讨论】:
-
按照惯例,类名以大写字母开头。考虑将您的类命名为
BadPlayer和Troll。