|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中( Y7 w. h/ R. O7 G
public void spreadMoney(int money){
0 p3 g4 C1 w; ~1 q0 C( m* g4 | // Randomly place money in moneySpace
' }" W! N* ?& g% V; }& }3 X0 ~1 [' {8 ] for(int i = 0; i < money; i++){0 ~, F6 d: s' s
4 a6 B' D$ v% ^3 Y) ~" K0 c // Choose coordinates! c+ p3 W5 m2 l
int x = (int)(Math.random()*(moneySpace.getSizeX()));
& o' S7 g8 F) z7 c3 L: O; _, p int y = (int)(Math.random()*(moneySpace.getSizeY()));4 Y6 k2 ?, P/ _+ w3 G' A
7 ]& v j9 k/ a; R7 v // Get the value of the object at those coordinates( {9 U" ?9 i5 A5 g: p3 u
int I;6 u% K: Y5 s! t* m+ c
if(moneySpace.getObjectAt(x,y)!= null){9 g8 u. H8 y" r8 g
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
8 T( I, j1 a9 ^ }
6 w& B* {# \# }* q* I* N4 T else{0 B4 S+ w! z" @& H* F% S3 Y; h
I = 0;
6 ?) @3 W6 Z0 P; p }: n% \9 f1 t- I3 w
// Replace the Integer object with another one with the new value
9 B2 q; W0 o0 z# T: p. e moneySpace.putObjectAt(x,y,new Integer(I + 1));7 C9 |# ^- v; `& V; ~" D/ I
}) _5 r, ?/ @4 j
这里面if(moneySpace.getObjectAt(x,y)!= null){
+ t3 r5 d" K6 x; t5 O I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();+ g" o; T# e9 f
}
% F1 H! p, B8 @2 X else{& o$ n! f/ b2 c% f |& m4 l
I = 0;! B! Q1 s! j7 |! |) P
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?7 ^- n) A0 l1 B3 J+ Z3 ]% D4 a
初学者,还请见谅! |
|