|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
0 i1 M8 l0 N/ B opublic void spreadMoney(int money){* x1 G$ l" b; G. m. [
// Randomly place money in moneySpace8 Y! {+ m6 Q2 r1 I1 P
for(int i = 0; i < money; i++){2 F5 s, G2 R$ t6 x- A
$ _1 v, h C! ~& Q# C% @4 g // Choose coordinates
( E- E) t0 R+ p. u" N int x = (int)(Math.random()*(moneySpace.getSizeX()));
/ m& W" {2 t+ R3 F. u% W2 l int y = (int)(Math.random()*(moneySpace.getSizeY()));
" k( U1 N9 t/ ^9 @, q$ M
^3 h, X2 c! u/ P' i // Get the value of the object at those coordinates
: ?+ x# M1 \* h6 Y8 c1 c% [2 ^# l int I;
, }! V; H/ Y0 _' j4 U) j if(moneySpace.getObjectAt(x,y)!= null){' f/ X, m) X! P) v+ e/ N- f6 c
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();( B; o4 u% ^3 `" J
}
6 P: [4 T2 C& ?- Q; g9 ^ else{* |/ x1 O. t8 D$ d6 F4 U: j
I = 0;
& B; L/ ^6 R- t }2 `3 s# h- m( ]2 Z( H& j
// Replace the Integer object with another one with the new value
3 v- z+ `# o. p+ v, |7 Y& z moneySpace.putObjectAt(x,y,new Integer(I + 1));
% T% f* S; b: i- s3 B. P5 X }
) H8 o% Y5 B* e( S4 M0 q8 }" J这里面if(moneySpace.getObjectAt(x,y)!= null){
% V& t( z6 H U" m I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
/ n) S/ h x' T) A }
}1 d8 f' C( |5 D2 o else{. X( r5 L3 x H4 T" {0 u
I = 0;
7 g1 S0 B$ g, @6 W& F- x4 l& o是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?) t2 b9 n9 L; K$ a6 q. C
初学者,还请见谅! |
|