|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中1 V1 z2 x+ J9 w7 N" w1 ^ \
public void spreadMoney(int money){
; g( W. g7 J% c* y1 S* _ // Randomly place money in moneySpace8 u# X: d$ j7 B- c. K2 E& M. d
for(int i = 0; i < money; i++){! o F9 e `# m8 T. M2 G5 S3 w
, b- b M& O- O+ J# b+ Z, ^ // Choose coordinates
. a; q# V$ c8 q" I& Y# U: d int x = (int)(Math.random()*(moneySpace.getSizeX()));
4 [! X1 X2 D8 [% M" l int y = (int)(Math.random()*(moneySpace.getSizeY()));
9 A3 g2 Z8 ~8 _# x+ E: E+ ]; G8 V* Q: p" e" l2 P
// Get the value of the object at those coordinates) O' d/ C2 O! G) q2 d0 M8 O6 @" x6 r
int I;% G, `' G" F& L: p
if(moneySpace.getObjectAt(x,y)!= null){
9 t: F$ c: W- p3 p- X' | I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
& y. ^; h# K, A! \+ ^2 s0 k- X- S }& |1 v: x4 s% c( W/ [6 p
else{8 e& L4 h2 r; p$ `/ U0 C
I = 0;# T, n8 c* c# r3 M
}/ Z: F/ l2 S2 p
// Replace the Integer object with another one with the new value9 G2 K2 D2 u- O
moneySpace.putObjectAt(x,y,new Integer(I + 1));0 F( {2 k- U9 K, k
}
q# b3 v& h& U0 }. p, I1 p {这里面if(moneySpace.getObjectAt(x,y)!= null){
0 R6 K C+ Q: d; |! a# Y I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
$ _1 U. l% s1 l" y: h }
9 L& b5 ]: a' F7 ?2 K9 ~& A* v# u else{6 k& a, g5 g) z+ v' I! S9 K
I = 0;
* u( d* S4 P6 @; O+ p4 _是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
0 Z& E. V6 G6 i3 \# v$ J初学者,还请见谅! |
|