|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
6 ?, t' r, W- l' w2 S/ l' kpublic void spreadMoney(int money){0 H0 d! B( |% r! \0 t
// Randomly place money in moneySpace
8 w& ^# w1 v$ M; f) l2 n, L8 N for(int i = 0; i < money; i++){7 ?/ v f7 y" Q
% u6 k7 y2 C6 t- Z7 w // Choose coordinates
! \) o$ B8 E- S6 n D' k int x = (int)(Math.random()*(moneySpace.getSizeX()));* d. p5 n- L% A3 t% R' N' d
int y = (int)(Math.random()*(moneySpace.getSizeY()));
" u5 Q/ p6 s- o) V: D' _+ ]% v
2 c# y: s! _- F+ r- v // Get the value of the object at those coordinates
; \6 O7 W; C4 ~) D9 E int I;0 U" l# A, j- I( m
if(moneySpace.getObjectAt(x,y)!= null){
( H; L. i f5 v& V6 d/ X+ D! A I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();) [: L3 ?9 P" r( `5 v& k7 U
}6 w Y; S( N0 x
else{
+ O& Y1 ^) E& Y; k- \7 K6 L I = 0;
, j6 Y/ E3 ^. Q9 r* t }- L+ P7 L) E ` H0 B
// Replace the Integer object with another one with the new value
r* i" X. P2 P9 B+ b! N6 U) E moneySpace.putObjectAt(x,y,new Integer(I + 1));1 M1 V' g3 }5 E' }
}0 r: }1 W* y6 C! B* h
这里面if(moneySpace.getObjectAt(x,y)!= null){
, X$ f9 c3 K6 K7 ~+ w8 p I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
- [9 u3 ]" m% F' l0 u+ z6 i$ [1 S( f# x }9 T* A% U5 B* V! Z8 a
else{3 F; R- C% c9 I- I5 y" ], @8 [, @
I = 0;- s) \# y! K) J' {) d9 W
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?& D5 }/ @5 T# W- p7 E* K
初学者,还请见谅! |
|