import java.util.Random;
public class Fighter {
int str;
int dex;
int hth;
Random gen = new Random(1535636);
public Fighter() {
str = gen.nextInt(9);
dex = gen.nextInt(9);
hth = gen.nextInt(14);
}
public int getHth(){
return hth;
}
public int getStr(){
return str;
}
public int getDex(){
return dex;
}
}
import java.util.Random;
public class Arena {
public static void main(String[] args) {
Random gen = new Random();
Fighter Guy1 = new Fighter();
int x =1;
while (x < 200000000){
x++;
}
Fighter Guy2 = new Fighter();
int hth1 = Guy1.getHth();
int hth2 = Guy2.getHth();
System.out.println("Fight Start");
System.out.println("---------------");
//System.out.println(gen.nextInt(10));
//System.out.println(gen.nextInt(17));
System.out.println(Guy1.getStr());
//Fighting
}
}
每次运行此代码,无论如何都会得到相同的结果。我希望每次生成两个随机的战士。目前有几行代码只是为了确认它不会生成随机数。
有人知道如何正确地在构造函数中使用随机数吗?还是我完全错了?