|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
1 i7 E$ t$ }/ H% O# {$ Opublic void spreadMoney(int money){" l& |4 \* T) ]% M" {
// Randomly place money in moneySpace- e5 S; ]/ Q3 ]
for(int i = 0; i < money; i++){8 }5 T1 q4 G2 f/ g( r
/ ^" R# X! J, I" { J! L! C
// Choose coordinates
* j9 r: h3 ^( @1 Q int x = (int)(Math.random()*(moneySpace.getSizeX())); G! D. J9 z2 U
int y = (int)(Math.random()*(moneySpace.getSizeY()));
$ E8 Z# x3 d2 I8 t" C
2 _" {) J3 u/ g# m- P; ^ // Get the value of the object at those coordinates; x! E7 A, U! m3 @2 f
int I;% t4 v- r/ x5 E. h0 v( V
if(moneySpace.getObjectAt(x,y)!= null){/ A$ b9 A8 j! l& T7 M0 r: l, v
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
4 k* y1 y9 O" N" f& g }& Y* ~/ V5 U! @/ R0 B0 L% a r
else{$ N7 \4 }8 L: ?2 |3 T+ U6 V1 G
I = 0;, u* B# H4 W& B2 {# j; W s4 o
}
1 z: s$ r; [$ i4 Z3 r4 S: a // Replace the Integer object with another one with the new value
$ C3 a1 H8 ]; ]2 i9 J moneySpace.putObjectAt(x,y,new Integer(I + 1));
+ c& |( q7 P& V0 J! M7 l }
: a2 B! z! O# J9 P0 x" r" v这里面if(moneySpace.getObjectAt(x,y)!= null){
4 d6 L2 m& l2 a( \6 B! p" o I = ((Integer)moneySpace.getObjectAt(x,y)).intValue(); D1 Y8 C4 T! d) y
}
0 H8 |% ?! X% e5 c else{- T/ x9 i( Z, _5 x1 v+ A5 N3 ?
I = 0;
" ^2 ]+ ]. s9 r1 E是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?+ U& U- l5 u$ I
初学者,还请见谅! |
|