|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
9 b1 q$ l$ k2 upublic void spreadMoney(int money){% M- V1 }/ D% F: e! m
// Randomly place money in moneySpace
G+ z! e3 z( H A+ B, p for(int i = 0; i < money; i++){$ {+ m# \) Z' s' x
0 M. f) |; [+ @" @5 X
// Choose coordinates
% c. j2 o) M. f/ } int x = (int)(Math.random()*(moneySpace.getSizeX()));% L! F3 G6 ^1 P
int y = (int)(Math.random()*(moneySpace.getSizeY()));: r1 I+ f% U! }! f: ~
$ v' i% X( Z& @9 ?) l // Get the value of the object at those coordinates7 l2 G5 m' x9 b% T
int I;
9 s3 q2 H1 \& y' Z- l if(moneySpace.getObjectAt(x,y)!= null){
* t( J9 `* m7 F/ K& { I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
7 i) u2 N) B1 X }8 G" P0 [' J( F: N
else{8 I: i& U2 D, Y' r" |
I = 0;+ f, g4 j1 W; Y
}1 z6 X2 w! J0 A. \) Y
// Replace the Integer object with another one with the new value4 U+ j. i6 \" \8 [( ]" g
moneySpace.putObjectAt(x,y,new Integer(I + 1));
, M; x! n- i8 f! d }
; c( J% t. _' X2 @7 G, C这里面if(moneySpace.getObjectAt(x,y)!= null){+ p7 m6 j1 G+ b) Z/ Z9 f. E
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();9 w: I( J6 S8 W% D6 _0 Z5 [ m% N
}$ ^" i3 N- d$ i
else{0 }$ v% }; I1 A
I = 0;# y4 I) V( Q2 o; A# s
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
$ ~* W( J+ S4 C8 Z0 W0 w% S初学者,还请见谅! |
|