|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中! @* h6 M; k% I- q+ _5 y
public void spreadMoney(int money){
$ n2 x4 R! u& Y7 J! N; J // Randomly place money in moneySpace* b1 K% E7 X* h, e- o+ f
for(int i = 0; i < money; i++){4 \) m- s/ ^- d' p9 q
) H( Z) P$ p7 Z" l& G. i3 i
// Choose coordinates
! \3 a) |$ x! h4 n0 L+ t7 P int x = (int)(Math.random()*(moneySpace.getSizeX()));
9 z) K2 d1 G5 B' h/ c! L6 X int y = (int)(Math.random()*(moneySpace.getSizeY()));# F. n0 D! k/ q) m) s
5 M2 J. X. Q0 f; q0 q! G
// Get the value of the object at those coordinates
0 P+ `0 u9 R" }8 W. v int I;
8 w# Y4 Q3 K4 y& }! Y3 W; Z' ] if(moneySpace.getObjectAt(x,y)!= null){1 C4 t, g0 L+ B" I
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();# G4 T# ~& T( c7 O. t- H* @
}
) l- I, A: k; A$ I else{ n' c7 `% W, j! t8 T! R: G! a
I = 0;& l! w3 W0 h7 @2 \
}) q, o* ~9 N8 P. N- t8 J' V: L {2 T
// Replace the Integer object with another one with the new value; y/ G- I! Y9 h$ e+ d7 S% g
moneySpace.putObjectAt(x,y,new Integer(I + 1));1 j- G, R/ X+ m& E, v
}/ _4 h: U8 x6 B8 Y5 H& B
这里面if(moneySpace.getObjectAt(x,y)!= null){
6 M; [) r3 ?! H9 s( L# k: ^4 c I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
; m) t6 Z/ Z+ R9 o8 m }$ ]$ {- Q) v# m' w
else{
' Y, G X6 U2 O. Z* B5 { I = 0;3 d& k) l0 S9 y
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?- Q! e8 @6 X, E& b# Y. d
初学者,还请见谅! |
|