|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
& }, H- h' I" I& ?8 ]public void spreadMoney(int money){
' Y4 Y0 j7 t2 v: H+ `: j6 q5 E, l9 ] // Randomly place money in moneySpace
. g# f4 m2 |4 X! s7 P: @ for(int i = 0; i < money; i++){
+ \, w9 r7 ~3 P% b8 E0 Y; w! q
+ ?" ]/ @+ H y+ W! T5 Q // Choose coordinates
6 l) ]' b8 t: A) \ x" J% f* r4 ~ int x = (int)(Math.random()*(moneySpace.getSizeX()));
2 V6 D7 E- O8 L int y = (int)(Math.random()*(moneySpace.getSizeY()));
7 H" ~4 Q5 i2 C7 }2 v
9 P0 _' _$ ]' h1 ^$ l! [ // Get the value of the object at those coordinates: D& [2 u" B4 h$ f4 G
int I;
1 E' {% W; v! x) e1 l if(moneySpace.getObjectAt(x,y)!= null){
6 B" Z8 C7 q p2 K4 {9 h8 V I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
p) X2 w; n, F* H& o. T1 C }
3 ?% u" w- S, l" y( ? else{
/ U4 N9 S, T; ?. L, r: T. N" |6 ~ I = 0;
% o0 H9 t I1 H: r }0 f, \* J8 K M! ~* [4 q1 U$ F
// Replace the Integer object with another one with the new value* N/ D/ B& y9 D' V8 M
moneySpace.putObjectAt(x,y,new Integer(I + 1));7 O8 s2 N2 X j/ D" q8 E" d
}
! t7 i2 K. O2 u0 I6 Z这里面if(moneySpace.getObjectAt(x,y)!= null){* M7 ^5 j2 ^" a7 f3 j- a ?
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
9 i, \! s4 K0 r5 }4 N" Q }) o5 p2 A1 |! i3 Y) T. a. `7 T9 D
else{8 k' g# W" ~ s$ Y* D" D
I = 0;
7 G1 F! w8 F! I是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?* M7 b, j1 d* Z$ y/ k/ S/ z; W
初学者,还请见谅! |
|