|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中8 Q* k+ k& ^: U4 E& U! M
public void spreadMoney(int money){" x& V0 x! [! o
// Randomly place money in moneySpace
% v9 y& H7 O6 E! I' z6 M for(int i = 0; i < money; i++){. x2 E% N) T! J2 f( c9 G) i$ M
) G$ A! D4 J& n3 j) }% G" [
// Choose coordinates
# y% f6 z& R7 r! ^. Y# q# u0 S int x = (int)(Math.random()*(moneySpace.getSizeX()));: s* P3 Y3 A) i
int y = (int)(Math.random()*(moneySpace.getSizeY()));
?. y$ z* Q# T& e' j# u) M. E/ E
3 `# u$ B7 I5 ` // Get the value of the object at those coordinates
7 h* m6 _& Y2 e+ I# D int I;
6 _+ i, I; {4 Q T if(moneySpace.getObjectAt(x,y)!= null){$ v* W8 f) Q, B, @2 N7 q1 ?. {" x9 h
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();* j( W* e1 [* i( b! S) ^
}
+ D3 M& P4 M$ m else{
! e$ [/ Z, V8 Y) k6 Q/ l1 {$ N+ h I = 0;9 Z6 y3 v$ V4 w. r
}) f/ E0 y, y7 ?" r8 S' r% o
// Replace the Integer object with another one with the new value- H8 x3 E& @: j2 z B- O0 n3 g
moneySpace.putObjectAt(x,y,new Integer(I + 1));# a: [+ f/ Q" L1 t
}
: F- _" `+ c5 @! ~- Y这里面if(moneySpace.getObjectAt(x,y)!= null){7 \$ M, o0 @ Z8 m( l, y9 r
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();( s7 _6 l V" i/ U! O
}7 e6 A8 A1 q+ t
else{
( n# @; Y, r- R1 r/ ^0 A# R I = 0;( w2 @5 N, J0 w# L% \! f
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
: `$ v3 t: w2 h! U1 @! r. Q初学者,还请见谅! |
|