|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
: T" [" B5 _. j. f6 Jpublic void spreadMoney(int money){- Q5 N9 X$ q9 X" b5 }
// Randomly place money in moneySpace
$ ~( T9 E) n' e4 Z" Q for(int i = 0; i < money; i++){3 i" d( V9 i- Y h" w/ _9 ^
/ ^+ f1 n$ v4 g* u; k; v // Choose coordinates
2 D. D2 h/ a# X9 P5 Y int x = (int)(Math.random()*(moneySpace.getSizeX())); g' z- n/ s' f
int y = (int)(Math.random()*(moneySpace.getSizeY()));! R6 b$ K! C$ L8 K8 D$ L) w: Y
: y- r% A' u3 u( F' P! ^ // Get the value of the object at those coordinates* d7 H% `3 `; N( E
int I;; Q; s2 Y$ \# @2 }. y }& c
if(moneySpace.getObjectAt(x,y)!= null){' n- y. l: p" v3 u) j' E0 c7 Q
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
. z# I0 X' p# g- w [. c }& {. p5 O6 c0 K4 l* @
else{/ o! i, }0 y' V! F
I = 0;1 Z& [. T K6 `3 a0 Q9 S
}4 b/ ^6 w0 K6 r) m* S3 K
// Replace the Integer object with another one with the new value
: y7 Z/ Y( g3 p moneySpace.putObjectAt(x,y,new Integer(I + 1));, s! O8 X: B7 J1 { H
}
: M+ S; h' @, O3 u7 z这里面if(moneySpace.getObjectAt(x,y)!= null){
# I4 y. m0 B5 Y; t I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();5 e$ n' e o- @
}& f8 M* v" j# {* S
else{2 J6 u, X/ m8 O0 H
I = 0;
: ]' R; e7 `! M3 d" ]; W& r: R是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?9 ~% x7 v$ w4 @$ M7 \! m {' Y
初学者,还请见谅! |
|