|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
0 a \# _! ^' r4 o: Y& W* Gpublic void spreadMoney(int money){0 J3 N" n8 c9 q9 a0 W# {$ D, x+ y
// Randomly place money in moneySpace2 w; o9 [; C* [, @, U# Z) e
for(int i = 0; i < money; i++){
0 r5 h, A% J8 V W* e1 B) c
2 Z+ u f! J6 k6 y // Choose coordinates
5 b) L, {3 V/ N) W int x = (int)(Math.random()*(moneySpace.getSizeX()));% J2 Y' a) v9 D
int y = (int)(Math.random()*(moneySpace.getSizeY()));7 i4 T+ L& P$ T3 E; D$ y9 M; b9 q9 V
7 ?7 g0 [: o) ~+ R% o! T1 k% s // Get the value of the object at those coordinates
5 S) Q' y2 K3 f# G% b int I;
$ r' d& i% {! [& G* g! N% u if(moneySpace.getObjectAt(x,y)!= null){
; B8 w) e5 g! s5 ?' k1 O I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();: d# ^" Z0 {* d( p: z5 l
}* q( Q" R, ~% C' ]" J3 L
else{
A+ ^' ?7 ?7 W0 q; u: L I = 0;
t& h' g1 A' F- w7 k* u+ n1 R }
) q' ]" j. f2 n6 x // Replace the Integer object with another one with the new value/ p8 j" @2 l* h9 x2 A4 s
moneySpace.putObjectAt(x,y,new Integer(I + 1));
% n8 @2 b9 ?/ K* K1 Q s" i }
/ E; e9 t7 `& |' O* [这里面if(moneySpace.getObjectAt(x,y)!= null){
: F- N' i% G) d$ O0 U O( @, n. m I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();; d6 e1 b* u- P( J9 L4 G3 M4 ?
}
* ^# M, I7 h; y- V" E& o else{2 l6 m; C3 o, W% R% V' `
I = 0;: _$ ^" B7 n' k5 e* W. J
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?4 r0 q" ^( U! {/ R% }$ I4 t# j
初学者,还请见谅! |
|