|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中, M) _% O! q2 o, V
public void spreadMoney(int money){ p) c- r+ O' N% S2 n% ?
// Randomly place money in moneySpace" j# m+ W$ L" ]# r
for(int i = 0; i < money; i++){
7 {7 o5 {8 q1 O( h* H6 ~# y5 j4 _7 O" z/ I) @; f; U1 _8 J/ q
// Choose coordinates
% X9 d( n5 i/ {) T2 n# p/ q int x = (int)(Math.random()*(moneySpace.getSizeX()));
! I, d# P2 b6 T( s& W3 @ int y = (int)(Math.random()*(moneySpace.getSizeY()));
3 T* o7 c9 g- D; V( q
( i0 B2 \% P* Z9 I5 r. n5 u! V- }; u // Get the value of the object at those coordinates
$ S' p3 }/ \( t int I;- [. Q0 _1 Q7 `
if(moneySpace.getObjectAt(x,y)!= null){4 a; g5 A6 `$ } i% d+ w) b
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
& _' I$ X5 L2 c- _/ z }: T3 u$ J: Y. e4 S" K" U: f; a1 N
else{8 j2 R& Y! C9 T; c* Y* ~6 r0 E
I = 0;7 L' n+ f2 x9 t
}
2 f+ [! a' z; g- |, K // Replace the Integer object with another one with the new value( A0 L% S+ Y5 K
moneySpace.putObjectAt(x,y,new Integer(I + 1));7 a6 J- W$ N! ~/ o- E3 v4 [
}
9 M( f1 w1 V/ Z% V' v& v% |这里面if(moneySpace.getObjectAt(x,y)!= null){' o2 _& x7 ?' t5 h: A
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
% s5 n% F3 F+ ~7 I! i }
; g4 g9 W+ R2 h ]5 ]( b else{3 Q6 b2 C5 u2 q( {
I = 0;# f. K3 S! V) o5 X" K" ~# {- K
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?" r% y9 \* x4 N, l4 `1 F
初学者,还请见谅! |
|