|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中; N1 ]; W" J1 m
public void spreadMoney(int money){
3 I& g; ^7 {2 i, Q' {5 v# x! Y // Randomly place money in moneySpace* R0 W9 j2 a+ p$ D5 [8 }) A& t
for(int i = 0; i < money; i++){( A: C. v" \* R2 C' Y& F- g4 `$ ?
5 j5 A8 V& c3 f9 F! t6 i // Choose coordinates
5 L: c/ T4 F& K, o. e2 l int x = (int)(Math.random()*(moneySpace.getSizeX()));
" }( l: t/ a1 t int y = (int)(Math.random()*(moneySpace.getSizeY()));6 P# q5 L* f2 y
6 m! R% U/ C' b+ y/ h- D- W8 o // Get the value of the object at those coordinates; T8 F1 Q& ]( \" ?% j, T8 Z
int I;' g+ y$ {: W- ^! [$ L0 O
if(moneySpace.getObjectAt(x,y)!= null){+ a% H$ \! q- \" Z. ?1 [- w, ~
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();2 \* g; ?' |4 J J9 Y
}
5 @4 [; u" o n else{0 n. v: \. ~- K
I = 0;
1 A- A. W q9 c }
6 W, c+ z& l7 S8 @1 g // Replace the Integer object with another one with the new value
" a2 I/ W# Z* `# T; c" J moneySpace.putObjectAt(x,y,new Integer(I + 1));" x3 ~4 q; J- W8 A
}/ x! l f# M8 D% E2 d K
这里面if(moneySpace.getObjectAt(x,y)!= null){
! b& \! ]& L' |5 p# L X I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
* b" P6 L5 d7 H/ J0 z; \0 D; v }
3 r% m' d- Q9 \* y x2 Q" m else{
8 ?$ e9 s+ {: z; M4 v/ L6 S7 p I = 0;9 s0 U8 D4 H( {; k$ Z; E
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?& ^0 ]( w4 C4 \
初学者,还请见谅! |
|