|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中! O) B' {9 \/ {4 O( D" l$ [
public void spreadMoney(int money){
- Y; }4 |2 g+ N+ w% z! F // Randomly place money in moneySpace
" n: l$ o" ^; T' M for(int i = 0; i < money; i++){
1 {4 S: `' T A' \# f; n) T6 _9 w. O- _% _( P. P
// Choose coordinates, x/ U7 O# H+ y
int x = (int)(Math.random()*(moneySpace.getSizeX()));, L- r5 ?5 D% _( Y1 X
int y = (int)(Math.random()*(moneySpace.getSizeY()));
; J# y( m9 C5 ?4 X. d
; Y" l' k k1 P // Get the value of the object at those coordinates! D& P! K ?' P2 P3 T- B& w
int I;. n2 f& G4 t7 K( ?3 i0 X
if(moneySpace.getObjectAt(x,y)!= null){
9 T' I/ C n; }9 l I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();6 q0 y6 ]2 v$ W/ N; P
}8 ~. u3 F. `$ t4 U
else{, `4 D1 u& y ?0 o
I = 0;; U f/ D8 v! [
}$ e9 Q" u5 v4 E5 {" S
// Replace the Integer object with another one with the new value8 V: T# z* q9 f8 e4 Z; ?2 z8 U
moneySpace.putObjectAt(x,y,new Integer(I + 1));* l! \9 g0 @8 Y6 `* W ^" S% M+ t
}% U9 u9 A! ^& L
这里面if(moneySpace.getObjectAt(x,y)!= null){7 n9 M: f1 V$ A- E, S8 |0 ]" U) i2 ^
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();/ N( f4 H2 `, B6 {% v
}
( G* u! l0 Z, q* F8 P else{8 ]0 o4 _! R. B8 c9 ^/ m4 D5 S3 ~
I = 0;
( l% D6 |- Z6 w: e, k: E1 q7 v是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
4 n4 p' x2 `5 n* A- |初学者,还请见谅! |
|