|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中& t: J) a" t1 m1 ^9 D
public void spreadMoney(int money){
$ I7 S3 Z5 a) ~- @( K // Randomly place money in moneySpace, [+ L! `( W# j, L2 t
for(int i = 0; i < money; i++){
2 h, U5 s$ m7 x$ A) \& q2 _9 z1 p) c
// Choose coordinates4 r) K- I& t" C9 n, a
int x = (int)(Math.random()*(moneySpace.getSizeX()));- ^( e5 @, n8 C
int y = (int)(Math.random()*(moneySpace.getSizeY())); b1 W; k* \: [+ D
! F* p2 u- [6 j/ Q/ i- `; k // Get the value of the object at those coordinates
6 N2 x# j( r& V- I int I;
8 J7 Z4 y& w0 s' c3 ?& l# C" n if(moneySpace.getObjectAt(x,y)!= null){
4 b8 A$ ]/ H& T7 n I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();& X9 l' O9 b" T) ?6 b+ ?! g
}
$ o1 [" M9 C* s) H& {) @( r& D else{* t+ U5 ~% F$ b. K" z3 o# w# Z
I = 0;+ ^: S5 a' x' C V/ r& d' h
}5 y5 l7 M3 }$ R$ F
// Replace the Integer object with another one with the new value
/ q a" a9 I7 t2 d moneySpace.putObjectAt(x,y,new Integer(I + 1));
! R9 M6 n! W7 `7 C3 j' o }
) b. V9 _- S, z; ~' n% F% @这里面if(moneySpace.getObjectAt(x,y)!= null){
& e, F( c8 ]- m( x! w I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
5 C' k5 o; I/ Q# j* e } R7 ~& w0 d. k E9 C4 d% c
else{
) t, h% S' B0 U I = 0;2 z2 Y! o$ @9 ^, J8 a
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?; z) A7 c/ m# t! \2 h7 F
初学者,还请见谅! |
|