|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
1 h" v6 [; k# w3 y: }; hpublic void spreadMoney(int money){* k. n+ ^3 b/ T
// Randomly place money in moneySpace
& q- l) k) T, I7 ?8 m7 H for(int i = 0; i < money; i++){
5 a/ K! U% V% H) m$ H/ _
6 r' H2 q. l Y/ I- ^ // Choose coordinates/ s/ f2 @* L$ _+ u
int x = (int)(Math.random()*(moneySpace.getSizeX()));
# x/ B4 G. N; c8 f0 I$ `% @ int y = (int)(Math.random()*(moneySpace.getSizeY()));9 u% `% ?4 @$ ?; Q% N" b0 V* a: V
1 M2 C' G4 ?5 i( \0 W7 {9 M! H
// Get the value of the object at those coordinates; d1 F. O' p) }3 E, j V
int I;
; q' J$ h1 r( m! ]4 T8 a3 r/ ^ if(moneySpace.getObjectAt(x,y)!= null){
1 q# b# L' N4 p3 K" \ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();. ?* d% o( [5 `5 Q7 f. u. z
}
7 r' O( D$ |: V/ N, u; u else{: A4 W# E$ \% ]# Q) h
I = 0;
; t; T7 w5 w& v }% N$ U" M! N2 Z. A K6 G; ^
// Replace the Integer object with another one with the new value
; z" o4 f/ J( @0 z2 M: S! n( I) L8 h moneySpace.putObjectAt(x,y,new Integer(I + 1));
0 u2 R& l- }4 A, |+ T: N& _1 ~4 S1 ^9 s }$ ^; s, \0 y) S
这里面if(moneySpace.getObjectAt(x,y)!= null){/ \7 ]3 K4 M9 |9 O, z6 Z
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();7 g0 v1 F; A2 ~1 j' u
}& {, c" V A9 q7 T0 r- t0 X& ]3 q
else{% M$ Y2 w; ~9 ]- v4 O4 @* t1 W9 j- |1 {
I = 0;, w0 L% B0 q5 N2 Q) s Y
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
* C" v% S; a; }- x6 T' I初学者,还请见谅! |
|