这是一个石头、纸、剪刀的游戏。我的问题是,如果我在私有 Random r = new Random(weapons.length); 中移动了武器长度。它会给我一个错误。如果我在方法中移动武器长度,它将成功运行。有什么区别?
public class Game {
private String[] weapons = {"rock", "paper", "scissor"};
private Random r = new Random(weapons.length);
public void thePick() {
System.out.println(weapons[r.nextInt()]);
}
}
对比
public class Game {
private String[] weapons = {"rock", "paper", "scissor"};
private Random r = new Random();
public void thePick() {
System.out.println(weapons[r.nextInt(weapons.length)]);
}
}