|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中; r; H$ r; z. Z- d
public void spreadMoney(int money){' I0 }* ]0 P7 Q' O5 C
// Randomly place money in moneySpace
t& H. U4 @8 ^ for(int i = 0; i < money; i++){
, f" `9 \ A# v7 Y- g, }
( t( w" v$ W) y2 M; }6 {( a // Choose coordinates
' s0 E" s5 R; t) j; V int x = (int)(Math.random()*(moneySpace.getSizeX()));- q. T# F/ N+ R0 K
int y = (int)(Math.random()*(moneySpace.getSizeY()));
2 R I! b* q# x# V% z0 w4 C. Z
& Q/ `3 l1 S6 B // Get the value of the object at those coordinates" E1 C, @) ~. z3 V
int I;
6 d2 }' {% k9 Z" G! |% I( V5 \ if(moneySpace.getObjectAt(x,y)!= null){9 R/ o0 h' [9 o2 i3 P% E
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();0 k3 [! I* J3 [) a& b) E
}
# o; [) Z0 ~4 ~, l* B else{
0 O/ ^' u$ l+ d4 x) e I = 0;
6 m$ t( @* Y0 m& A# ^3 t. e }
3 W) x8 z9 K. @0 d // Replace the Integer object with another one with the new value
7 n) S6 x- L) v( j( ^ moneySpace.putObjectAt(x,y,new Integer(I + 1));
& B2 w5 E1 Z9 S8 w7 ~ }, r' h; d S: W! w# p
这里面if(moneySpace.getObjectAt(x,y)!= null){& \5 G+ P6 e0 A8 w: B
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();9 J; r: e! H* x+ h8 m2 J! t
}6 b' |% ^4 l; d" s" k" Q- _
else{' I' o7 E: V4 h. U) O6 q/ A
I = 0;, X. |* s" t0 y [" i5 ^% w
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?1 D3 ]7 \% c( [& X
初学者,还请见谅! |
|