我正在尝试为派对创建模拟。将有4个班级:主持人,工程师,艺术家和科学家。其中每一个都必须放置在50x50的场地上。为了做到这一点,我必须创建一个随机发生器,因为我之前从未做过随机发生器,所以我真的坚持了这个部分很长时间,所以我希望能得到一些帮助。 (我是初学者,是的。) 我目前收到此错误:“线程中的异常”主“java.lang.NullPointerException 在Simulator.populate(Simulator.java:88) 在Simulator.partyStatus(Simulator.java:101) 在Simulator。(Simulator.java:56) 在Simulator.main(Simulator.java:41) 建立成功(总时间:3秒)“
如果有不同的,更好的方法,那么我会非常乐意看到它,如果你需要更多关于此的细节,那就问一下。
提前致谢!
public class Simulator {
private static double ARTIST_CREATION_PROBABILITY = 0.2;
private static double HOST_CREATION_PROBABILITY = 0.2;
private static double ENGINEER_CREATION_PROBABILITY = 0.2;
private static double SCIENTIST_CREATION_PROBABILITY = 0.2;
private static int seed;
private ArrayList<Artist> artists;
private ArrayList<Scientist> scientists;
private ArrayList<Engineer> engineers;
private ArrayList<Host> hosts;
private ArrayList<People> people;
private int depth;
private int width;
private Field field;
// private static int step;
private SimulatorView simView;
Random rand = RandomGenerator.getRandom();
public static void main(String [] args) {
// SimulatorView simView = new SimulatorView(50,50);
Simulator simulator = new Simulator(50,50);
}
/**
*
* @param depth
* @param width
*/
public Simulator(int depth, int width) {
this.simView = new SimulatorView(depth, width);
this.field = new Field(depth, width);
this.depth = depth;
this.width = width;
this.partyStatus(simView, field);
RandomGenerator.initialiseWithSeed(seed);
}
public void populate(Field field){
// People host = new Host();
// People artist = new Artist();
// People scientist = new Scientist();
// People engineer = new Engineer();
// field.place(artist, 10, 10);
// field.place(scientist, 20, 20);
// field.place(engineer, 30, 30);
// field.place(host,40,40);
Random rand = RandomGenerator.getRandom();
field.clear();
for(int row = 0; row<field.getDepth(); row++){
for(int col = 0; col<field.getWidth(); col++){
if(rand.nextDouble() < ARTIST_CREATION_PROBABILITY){
Artist artist = new Artist();
artists.add(artist);
field.place(artist, row, col);
}
else if(rand.nextDouble() < SCIENTIST_CREATION_PROBABILITY){
Scientist scientist = new Scientist();
scientists.add(scientist);
field.place(scientist, row, col);
}
else if(rand.nextDouble() < ENGINEER_CREATION_PROBABILITY){
Engineer engineer = new Engineer();
engineers.add(engineer);
field.place(engineer, row, col);
}
else if(rand.nextDouble() < HOST_CREATION_PROBABILITY){
Host host = new Host();
hosts.add(host);
field.place(host, row, col);
}
}
}
}
public void partyStatus(SimulatorView simView, Field field){
this.populate(field);
simView.setColor(Artist.class, Color.ORANGE);
simView.setColor(Scientist.class, Color.BLUE);
simView.setColor(Engineer.class, Color.MAGENTA);
simView.setColor(Host.class, Color.GREEN);
simView.showStatus(1, field);
}
}