|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
2 k, i) [4 X( P5 Q* y6 c: apublic void spreadMoney(int money){
) A5 t0 E) M. Y5 R3 K8 `4 y // Randomly place money in moneySpace
+ e0 I4 Z1 X; w1 m3 {. u for(int i = 0; i < money; i++){
. h# A0 z* ?5 P( y% e8 ^) v
' J8 Q& j3 a) |/ j- V# e // Choose coordinates
. S1 u* I3 u2 f* @ int x = (int)(Math.random()*(moneySpace.getSizeX()));0 v% p9 t/ I. W# {0 D
int y = (int)(Math.random()*(moneySpace.getSizeY()));2 `, S( V2 D7 w1 [; h" s! n4 L8 y
# W8 n" h( r ?" G- u4 J+ y5 p$ U# T. O
// Get the value of the object at those coordinates
# [/ D ?- h* @% j2 t2 ] int I;
! W) m* W" V0 V2 b* `9 t if(moneySpace.getObjectAt(x,y)!= null){4 e+ `' C, g! D- o& k* g
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
5 x/ |' w: _$ t8 g6 u& ~! |, N* o }
/ i8 C8 O' p0 u+ A8 V K8 w9 ~& C else{
* p/ B. l" @% ]2 v2 N+ V) a2 { I = 0;
9 e1 e0 U! U8 g }+ z( b* O% P7 T1 g* @
// Replace the Integer object with another one with the new value$ g! J* T, j" j0 P- y* r( q/ Q
moneySpace.putObjectAt(x,y,new Integer(I + 1));+ i6 R, e% _+ n
}
, f7 i- _" Q! E9 @这里面if(moneySpace.getObjectAt(x,y)!= null){. l* ]/ M) A7 a, d3 n
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
! w9 K( {# u* C6 {5 n+ n1 z }# a% k( W2 E' H
else{
& w! x8 ~! }0 H9 ?7 P3 S; C( } I = 0;+ e! f% h" Q: e" r
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?# [: F/ D5 t$ H# q U
初学者,还请见谅! |
|