|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
( i2 q, [* a: t5 u! w6 ?public void spreadMoney(int money){
$ }, q$ o: q& y+ _$ _ // Randomly place money in moneySpace+ r T, K) @4 o4 P' U
for(int i = 0; i < money; i++){
7 S' i9 x3 A" P6 J9 m* j; l! p2 p) ~4 `; O3 B6 ?, F
// Choose coordinates) P8 ~ f# P+ a2 S; I
int x = (int)(Math.random()*(moneySpace.getSizeX()));
; w: ~- q; v. H. G; _0 c" y k3 H int y = (int)(Math.random()*(moneySpace.getSizeY()));
# @* D9 c' t6 Q& ]0 d! H; W4 M* c" C" e' j. ~# k$ @; k1 B7 z7 f
// Get the value of the object at those coordinates
; j+ n- r' x1 s# V int I;
1 k' t4 R- E6 l6 L& b6 O$ O. T1 ^ if(moneySpace.getObjectAt(x,y)!= null){( ]1 Q. E# v/ \* s
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();: ^, j h& N1 z
}# S3 D# X5 {0 D, d' Z7 w2 V0 K
else{
, R" {( G) R- i0 S6 H& Z I = 0;
2 A& |& N! c5 W1 Y }
2 A# K, F0 a8 n( \" n // Replace the Integer object with another one with the new value
/ n8 `- G" ^- o3 K1 i3 k1 A moneySpace.putObjectAt(x,y,new Integer(I + 1));
2 }; d& ]$ @4 k4 }) a }/ ]( ^ n/ X* \' e8 e+ N" V/ h+ I
这里面if(moneySpace.getObjectAt(x,y)!= null){
. z8 i$ d- m" c& L( X1 ?2 l I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
* [4 }3 Y8 ]% ?9 [, P4 W }
, J3 v5 \( I& k else{
7 t5 `$ b$ E9 e: g( x. D/ }+ \ I = 0;4 ]% e0 G8 s: s& e4 _ h) `
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
/ z: J4 o; ~) q初学者,还请见谅! |
|