|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
" L" B0 [- f1 lpublic void spreadMoney(int money){$ k# [- }: D& h2 s) |! ?
// Randomly place money in moneySpace
- x" F* V" p. b* A for(int i = 0; i < money; i++){
2 C! U- H6 f. S( ?& g/ u2 a* `2 a0 H& U( T
// Choose coordinates
" b; S2 i; R8 S0 h0 s/ c( V0 B int x = (int)(Math.random()*(moneySpace.getSizeX()));4 J" `6 m' O* g ?' K; ]5 T7 B1 h' y
int y = (int)(Math.random()*(moneySpace.getSizeY()));
+ a5 X4 r |" W. n6 `1 |" p. ~- c. d
// Get the value of the object at those coordinates
( X( O# f n- L; }1 ]& } int I;
# u7 o! j& P- Q% [ if(moneySpace.getObjectAt(x,y)!= null){
3 ]/ R V, X6 k. u, |6 Q3 R I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();: ]2 L7 f- r" m
}
1 m3 X4 J# E/ p8 n. O" s/ J+ c9 |" C else{) s% b# e* t; ^5 U2 \2 N* K' h' W
I = 0;7 z/ `- P8 M: E
}
f7 c$ w, w) ^! y6 S // Replace the Integer object with another one with the new value1 x8 _9 ]% ~& t: j% k
moneySpace.putObjectAt(x,y,new Integer(I + 1));
5 F q; Z9 x+ j }4 D* e8 t. O$ q/ ?; V' v
这里面if(moneySpace.getObjectAt(x,y)!= null){
( i# w; N) x- N8 h- l% L1 _ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
% [3 U4 D4 }4 a% f9 j) K0 E/ m5 H. \2 t7 d }9 F. ~! u- ]& _& k
else{8 ?& Q; d- D2 {
I = 0;
) H& I: N8 { t3 a& s! q是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?5 N. V/ D& U# n4 P
初学者,还请见谅! |
|