|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
9 F( N9 N2 Q$ E5 s8 c; Kpublic void spreadMoney(int money){2 c! ~, O9 _, |" D
// Randomly place money in moneySpace
# V. f0 u% W0 n4 P$ {' P6 A for(int i = 0; i < money; i++){: T, @: D, S& K; b$ q
$ U7 t: o! R" l0 |9 b // Choose coordinates
: E! d" G2 o+ s int x = (int)(Math.random()*(moneySpace.getSizeX()));
3 Q2 D" f; n( b int y = (int)(Math.random()*(moneySpace.getSizeY()));
# d7 p. n+ V4 K0 Q# f+ A7 C5 L8 v0 `( q0 _+ b/ f0 ~
// Get the value of the object at those coordinates
, n3 U; T/ W5 g2 L ]' w( e int I;3 ^* H: A+ C1 T/ }7 F
if(moneySpace.getObjectAt(x,y)!= null){
4 N/ V3 h: c' E8 Q I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();- \+ }. M5 o$ d
}
; f) r F3 }" V! } else{
. _2 [; m" `; j+ w# j3 g I = 0;! ] O4 k9 S+ {7 m- M1 ~
}
1 _' O( K" D) o- P! U& v // Replace the Integer object with another one with the new value7 O, N, s5 g) n! ?) a1 x: k8 R
moneySpace.putObjectAt(x,y,new Integer(I + 1));8 ~3 E- W8 z* q( _3 q/ R
}
: j2 @$ [& [& k这里面if(moneySpace.getObjectAt(x,y)!= null){# T! i1 G1 ?6 }
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();2 _ W. P$ ~/ K" V1 q, y
}
) w4 k; [% x5 c0 ^; e else{6 s- F' f }# E# t
I = 0;
+ Z4 V t# g8 a- E是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
1 B2 R" e9 A$ ?初学者,还请见谅! |
|