|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
6 n: z E# k: W i* L Tpublic void spreadMoney(int money){
# S: e2 b$ R) x! ] // Randomly place money in moneySpace
% V0 e7 W' R# K% | for(int i = 0; i < money; i++){
0 D- k/ t7 [" L
* f M( ^% K6 M U9 L$ U3 n // Choose coordinates( n8 g: x7 ?; x2 D+ O0 h
int x = (int)(Math.random()*(moneySpace.getSizeX()));/ p3 ]; G2 S: v5 {* _; M1 }
int y = (int)(Math.random()*(moneySpace.getSizeY()));
; V4 m, a) }& H
! M+ z1 _5 K5 T! r, M) g- Q // Get the value of the object at those coordinates
" f$ G. ^! S! K int I;
4 T3 G" l' X; e' a if(moneySpace.getObjectAt(x,y)!= null){9 {8 W0 U g+ F! a
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
& l3 G" P+ m- a U9 ^# G, n/ ]" h }( i' M: z" |/ |0 A! v
else{
% }. y5 @. K' Q- R# P# x I = 0;* [/ F6 B5 V* R6 {7 u0 H' D
}
3 W& Z2 {3 c+ q! q1 t! c* T+ u // Replace the Integer object with another one with the new value3 f" x2 Y5 m& w% Z
moneySpace.putObjectAt(x,y,new Integer(I + 1));' i+ z! d4 _* r* t
}9 ~- P% z3 Y; b5 a v$ M: k6 E% \
这里面if(moneySpace.getObjectAt(x,y)!= null){
/ C& K5 i. ~! u I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();6 I/ g9 S( r1 q9 p+ |+ p
}5 A5 u6 L5 x9 e/ O/ y) K
else{* e; O) [0 }, j( s' V0 ^6 }
I = 0;. u7 k4 B' z% s3 b+ l3 K
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
8 r6 [: _: P% d4 m# t7 e( @( X初学者,还请见谅! |
|