|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中: N- E6 A1 S% \. M; R- K: B# ]
public void spreadMoney(int money){0 Z/ l" |% V3 D7 w- y# q! _7 ?4 H; D
// Randomly place money in moneySpace- e+ x) b' _) ?9 f
for(int i = 0; i < money; i++){
6 K `8 w7 m. [+ k' p& c
! L6 N: p$ |: o* H/ C; s // Choose coordinates7 X0 g Y1 H: E1 e/ o
int x = (int)(Math.random()*(moneySpace.getSizeX()));- ^' ~; C9 i- \& ^3 s
int y = (int)(Math.random()*(moneySpace.getSizeY()));& N! U! {% J6 b/ V$ }8 P7 f/ |
" J, u6 e* f% O, J# y8 N // Get the value of the object at those coordinates( S0 P, Y" A6 E" y
int I;. e( E& a+ B7 x( i! Y
if(moneySpace.getObjectAt(x,y)!= null){
( n% s, S' C; m I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
5 @1 _ t3 q2 Q( M' J }" f% O1 b9 q- O4 j7 Z3 {* ^
else{
2 ?( V% Y. Q2 a5 y I = 0;
7 `! B! L: g; { g }
# f7 ?1 }) b, S$ |& M& Z' q // Replace the Integer object with another one with the new value
( m- Q y+ K# p" U9 | moneySpace.putObjectAt(x,y,new Integer(I + 1));
7 k6 D3 a3 k6 d. Z }- A0 W! c4 L7 n
这里面if(moneySpace.getObjectAt(x,y)!= null){
/ w% P1 ~0 N' O9 W0 A( w4 J6 b I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
! k6 F3 \, u5 F1 k+ ` }
3 B) w7 `# j( }% A/ o else{
9 n: y: r" N+ |3 u; c) e I = 0;1 v: h$ h: A" x. C
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?6 j2 x" R: L: _3 t0 d! H& N
初学者,还请见谅! |
|