|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
) J2 T2 i1 j% [& Y# H7 zpublic void spreadMoney(int money){ Y. M9 ]7 Q" `* Y+ ]9 p
// Randomly place money in moneySpace5 `, V. W- c! v
for(int i = 0; i < money; i++){6 {% E% t, X" t) S, ` t/ h1 y
$ n$ k3 D; k. I // Choose coordinates
. m6 s* k' X8 `% D8 n; Z1 R int x = (int)(Math.random()*(moneySpace.getSizeX()));
3 X# K$ A! }/ M6 |4 g int y = (int)(Math.random()*(moneySpace.getSizeY()));! `% t; b' O* K$ X' E7 R
& g2 h2 Q# }9 u ~
// Get the value of the object at those coordinates
1 ^7 b# N( Z: `* { int I;
0 I4 s* p( v# y7 r$ G7 a6 ]! w! D if(moneySpace.getObjectAt(x,y)!= null){
2 W1 I b' J; P/ F I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();, f5 E( R( w: H# ~: K4 I8 k9 u
}& E4 c: X& ^2 h+ W7 f, i6 \
else{
: z' `) r4 y' j: I I = 0;/ i, a6 ^& T6 r" f5 W& H: {. o
} ` ?1 l0 Z8 w" }% Q& g2 B4 K
// Replace the Integer object with another one with the new value
1 \$ l7 Z4 D: g; i( R moneySpace.putObjectAt(x,y,new Integer(I + 1));% N0 g- k1 u% D" M- x% D: u
} d- L/ z4 M1 ]. a/ J$ w) W
这里面if(moneySpace.getObjectAt(x,y)!= null){. U( ?$ F( k5 H/ m& m
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();, F5 g6 F& c" `1 ]. k$ w2 ?2 U
}0 z' m5 X9 C0 U! Q' b; v
else{
' x( S5 R g8 @1 j6 _ I = 0;/ }- K! c6 V: V# W; p
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
# B; `; J; |$ g* f0 Q! ]$ U$ O初学者,还请见谅! |
|