|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
* ]3 E& B7 e# S/ Fpublic void spreadMoney(int money){
' m: @: L+ M) N0 T) | // Randomly place money in moneySpace- i5 a/ O( a3 S) X2 I# S
for(int i = 0; i < money; i++){ S4 C+ y/ y! t3 F
# _, B2 V5 e) X6 _1 G
// Choose coordinates
( r. k, V$ q/ R% l0 S5 r5 c int x = (int)(Math.random()*(moneySpace.getSizeX()));
4 z0 Q7 Q0 X. J/ n O int y = (int)(Math.random()*(moneySpace.getSizeY()));
+ E5 B. M0 q1 |0 d
; i+ ]! c! v- p# y/ [" D // Get the value of the object at those coordinates
3 h( i) h! f2 A! W4 h int I;
. b* g b. a3 E( q1 l if(moneySpace.getObjectAt(x,y)!= null){! ^& Y# N& N" v9 L
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
9 q' c2 w- v" B Z }
0 w1 F. G s9 D# W; l) j- z$ S else{
9 r6 \5 j, }% i I = 0;
/ V. b6 M$ o( W' B1 Q$ ] }
" V0 e" k/ @2 V // Replace the Integer object with another one with the new value/ M% `3 x2 g& D
moneySpace.putObjectAt(x,y,new Integer(I + 1));3 }# n; }' ^4 ~8 S) M
}! L- L7 Z5 [' \. J' C4 Y
这里面if(moneySpace.getObjectAt(x,y)!= null){# T# j6 X# L* @1 n
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
! s/ L, X& }: o3 ^: P }9 O: i) d% w; N r# ~; V0 d% U
else{
) D! i- c: i3 ?+ m9 Q I = 0;
2 U+ Q& W/ T+ f是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?* v' V4 V2 V7 [% A
初学者,还请见谅! |
|