|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中0 D" h+ I. `3 @8 h9 D$ |$ D6 \
public void spreadMoney(int money){5 j6 K0 q _7 m
// Randomly place money in moneySpace
1 Y6 J4 P3 @& U! v& |% O for(int i = 0; i < money; i++){
- p/ p8 s; t1 e/ c$ @& z, V, _ F, o$ C( B
// Choose coordinates) w F+ `1 r6 n+ h/ h6 Q
int x = (int)(Math.random()*(moneySpace.getSizeX()));
0 K8 f) a/ K! x3 ^7 ?& j" f int y = (int)(Math.random()*(moneySpace.getSizeY()));
% _- E a/ n: h& K. }7 w8 I7 p9 g$ c$ }, e
// Get the value of the object at those coordinates
+ C4 Z2 G& ]& m3 f$ p- M, D int I;
3 }- V* ~/ V9 V if(moneySpace.getObjectAt(x,y)!= null){
8 o( `0 c D0 E0 W6 a6 | I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();7 A( O, Q6 n5 x% C5 d8 g0 O9 N
}
( G9 O1 t0 ~1 g, l }6 ~4 \ else{
8 {4 ]# H: |" V$ }$ ` I = 0;1 p$ V* l# ^. N3 b9 S* ?7 Q* r
}
4 |! |2 @! g+ Z ~1 M3 s1 P // Replace the Integer object with another one with the new value
4 u4 Y" w4 k2 @1 g2 g! H moneySpace.putObjectAt(x,y,new Integer(I + 1));' W i" G1 q% A- h
}
i& \- x0 v6 a" q这里面if(moneySpace.getObjectAt(x,y)!= null){
5 f4 ~1 z7 i3 g# A K I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();3 N$ Q( r$ i" V: V3 b- `; C6 }
}
7 n# X h8 {( z else{
9 H" I2 S# E( D/ f5 C" m I = 0;; L, j6 Z W5 C, M! N7 F
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
1 E) e$ t0 K6 ?1 t8 h! ~初学者,还请见谅! |
|