|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
& c" I$ e- }+ G! ~1 Kpublic void spreadMoney(int money){5 e' G" O& n6 R+ j
// Randomly place money in moneySpace5 J2 \) t2 b8 A4 N4 W( V5 a
for(int i = 0; i < money; i++){
. p9 i1 Y3 c. g8 Z* U
7 v2 L$ s/ Z, D8 E) o' { // Choose coordinates/ W/ Y: P7 I! z
int x = (int)(Math.random()*(moneySpace.getSizeX()));
7 _6 t# b: V! B2 z7 }, B+ f int y = (int)(Math.random()*(moneySpace.getSizeY()));
) R* `" u e; y2 H7 |/ `
* h& F1 m9 O" P // Get the value of the object at those coordinates
5 @1 {- m$ a8 `, @ int I;' j' j% d/ K! V
if(moneySpace.getObjectAt(x,y)!= null){: L( z: y' h. G1 M
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();8 V; `; H. `, n. T3 }% s
}
! R8 {0 M% s8 D else{& v3 C: ^* h: D' [6 N8 @# ~
I = 0; R( r& y+ Z( a
}+ n4 D! _$ x( g& _7 ]( d
// Replace the Integer object with another one with the new value( M. A+ n7 |" V$ U, ]( ^: C6 _
moneySpace.putObjectAt(x,y,new Integer(I + 1));
/ m; r: e2 g) t7 U6 ? T! }4 E }5 M+ }' Y( S4 X0 }$ V
这里面if(moneySpace.getObjectAt(x,y)!= null){
& K3 A$ o# ^$ K: @! l6 g I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
7 t& B$ ~0 Q* n( f, M2 _ }% H5 k* \8 E$ o* q; E# t
else{
6 r( V2 Y% U! C2 c I = 0;) a$ Q# A- C' K8 X, O, {. _
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?8 `2 R( { y. o, M9 }5 `
初学者,还请见谅! |
|