|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中, M0 j& R1 W7 }& {4 ~
public void spreadMoney(int money){- B! C* U5 Z! f2 m) e& }
// Randomly place money in moneySpace# _# N# V; o A% Q
for(int i = 0; i < money; i++){, ]0 J% L# l! {: ^9 k
. E. W+ I# z) t6 R5 |" p h
// Choose coordinates$ K8 `! Q3 \0 I$ o
int x = (int)(Math.random()*(moneySpace.getSizeX()));* n! t+ `" o* t1 T5 h0 d8 a" h6 }
int y = (int)(Math.random()*(moneySpace.getSizeY()));8 f0 J1 r4 K8 Y* y3 w; y/ m; J
J1 [- B/ z0 K' b5 g& l; `# B
// Get the value of the object at those coordinates/ H- Y) m o7 ^4 B. d0 ], ?
int I;
; ^+ D; E* g# b. o" U if(moneySpace.getObjectAt(x,y)!= null){
( j0 M! i7 u% B% [ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();( T3 \) H8 B1 i% [; T5 M
}* I y; `3 C# @6 W5 B' u
else{1 }! @4 U& n# H5 k* l1 \# F7 w$ b
I = 0;. y0 v7 G: u6 t: `& y( [: V
}; [, ]" o6 k- a# A$ P
// Replace the Integer object with another one with the new value- q$ J# {' m( M7 j/ M
moneySpace.putObjectAt(x,y,new Integer(I + 1));, u0 A/ S, v' G# j, I
}& Z4 Q8 Z6 Z( Z+ z- O! o. K
这里面if(moneySpace.getObjectAt(x,y)!= null){* v9 I0 W8 X1 c* T+ }
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
: o% C( I' z! J4 h0 z. @1 i }* {% H: P7 `( @0 q
else{5 d& Y* w# b5 M$ |
I = 0;
& j* ]6 O7 `; L% g! Y0 ^是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
6 L* C) x( U1 o7 U1 `1 ?5 J! H初学者,还请见谅! |
|