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
}
}
每当我运行它时,无论如何我都会得到相同的结果。我希望它每次都能制作 2 个随机战士。现在有几行只是为了确认它不会产生随机数。
有谁知道如何在构造函数中正确使用随机数?还是我这样做完全错了?