应该是个简单的问题
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中public void spreadMoney(int money){
// Randomly place money in moneySpace
for(int i = 0; i < money; i++){
// Choose coordinates
int x = (int)(Math.random()*(moneySpace.getSizeX()));
int y = (int)(Math.random()*(moneySpace.getSizeY()));
// Get the value of the object at those coordinates
int I;
if(moneySpace.getObjectAt(x,y)!= null){
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
}
else{
I = 0;
}
// Replace the Integer object with another one with the new value
moneySpace.putObjectAt(x,y,new Integer(I + 1));
}
这里面if(moneySpace.getObjectAt(x,y)!= null){
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
}
else{
I = 0;
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
初学者,还请见谅!
页:
[1]