|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
& i/ K/ i$ ~2 I1 C- fpublic void spreadMoney(int money){ b ^' k! R; A1 y* X
// Randomly place money in moneySpace
' |2 D: Y- S; } for(int i = 0; i < money; i++){
' p R8 A/ |$ R
0 J+ t1 V3 k) u& M // Choose coordinates2 j3 j. E, U8 S4 i( ~
int x = (int)(Math.random()*(moneySpace.getSizeX()));
" c& l/ m: x9 g. P% v+ L" t int y = (int)(Math.random()*(moneySpace.getSizeY()));# ~+ j8 [# C- k5 v. R1 ^
: x0 i) C( r/ w" _3 L5 q" \
// Get the value of the object at those coordinates
. Z% S! I# T. E Y int I;
/ s4 z2 R9 ~; E o1 n. e if(moneySpace.getObjectAt(x,y)!= null){% s% x1 `7 K+ H6 r' a
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
* G# B8 A: \4 r$ w. U }
2 q# J3 N; Y& I# f$ e' t8 o else{
7 e- {' B( b0 k I = 0;
, M# j7 r8 v: K* n }
r3 x: C1 y3 u // Replace the Integer object with another one with the new value* ~) z9 G9 e, ^9 S( j
moneySpace.putObjectAt(x,y,new Integer(I + 1));) @8 A9 Q; C5 U
}
^6 E P' b0 Z这里面if(moneySpace.getObjectAt(x,y)!= null){$ @+ X2 U( [ B, M! c' k* S5 Q
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
9 ~( h( r1 f3 V }
' o1 {* d. N$ u" C else{
3 ` }) Q9 O7 o/ F! J& j, U4 a& x3 A I = 0;
9 C/ S7 j+ a" Q8 z' g" a& S. H是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?5 ?! K' G3 n/ k* O1 t
初学者,还请见谅! |
|