|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
) o4 O" W h: s5 vpublic void spreadMoney(int money){
8 v, f5 \3 u* | // Randomly place money in moneySpace9 [( N' Z7 C* z8 Q# k& }0 i3 Y
for(int i = 0; i < money; i++){
* Z2 R" z/ U6 W! ~) N2 d7 J" Z' z: |% O. g. Z4 B; i
// Choose coordinates/ O/ A' o j+ r5 x6 O
int x = (int)(Math.random()*(moneySpace.getSizeX()));5 i( x2 D( r1 i; g$ I, m3 y3 \
int y = (int)(Math.random()*(moneySpace.getSizeY()));
* u8 v5 c$ j ~; `9 u' D) T
8 b) k6 [; U% |; u. J1 k // Get the value of the object at those coordinates
$ k2 G: F" ?0 L. {: g5 @- I int I;
- `5 F: h8 ^; V9 G3 O/ @ if(moneySpace.getObjectAt(x,y)!= null){
( b1 ~" w0 [4 N' J. b0 @0 p: _9 M I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
! P u7 P' l) z. _" W6 u( L$ n }$ ]- b8 E' f) E2 T5 _
else{
) ~# L7 `" ~0 V. C- E I = 0;
9 }* `1 t3 S/ o5 \/ t. ]9 ~$ v }/ A; ?1 `8 O" O
// Replace the Integer object with another one with the new value
+ I ?9 |1 H# g2 \4 _2 y" z moneySpace.putObjectAt(x,y,new Integer(I + 1));/ M5 V4 x6 p8 g+ c" L3 U J
}' i" R6 E8 }2 C: F
这里面if(moneySpace.getObjectAt(x,y)!= null){" ~9 F7 J0 g" n$ @# ?) n
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();( P+ R: x2 M) _
}9 v6 t% D8 ?& V% I% j
else{
6 m) M# c7 S2 x& w; W7 k I = 0;6 A9 V/ k' ?( v. b2 k
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
& _; \: r0 ^; B w) H! f% u初学者,还请见谅! |
|