|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中7 m: S4 ^% P- G: P) M
public void spreadMoney(int money){* ?+ P- u9 e% k
// Randomly place money in moneySpace8 Z& {+ B% k5 k
for(int i = 0; i < money; i++){) u1 L* f0 T4 b d. [
3 n% ?% F( k9 p# k7 r
// Choose coordinates
/ E* r4 x. b, S" S- { int x = (int)(Math.random()*(moneySpace.getSizeX()));. y. {6 c% j0 J8 A3 J3 k
int y = (int)(Math.random()*(moneySpace.getSizeY()));
) w$ V" W, |% w9 B4 G( Q/ n
: S8 j% [) w0 ^5 c+ O4 s2 C/ M // Get the value of the object at those coordinates+ t& k! D4 i$ p- T: \$ ~( w
int I;
8 X, t/ T" a* N, N8 P if(moneySpace.getObjectAt(x,y)!= null){
6 C+ m) S7 w4 R1 n# ` I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();% K/ R' j. n2 M: X* t& \
}
. N8 T4 t# |- o/ z4 X; V else{
* e/ |7 J+ M5 J6 P# K. o6 A I = 0;' S- H0 _; h$ [, z' `) x
}2 _0 g. {- I, T
// Replace the Integer object with another one with the new value* n7 q y1 N+ J: v, `$ D% k
moneySpace.putObjectAt(x,y,new Integer(I + 1));
3 x. ?1 |- ^1 |. O }: U6 e/ ?8 ] t `9 O9 P
这里面if(moneySpace.getObjectAt(x,y)!= null){
( X8 Y( p& B/ f" z& O" ^0 B I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
+ X8 [ N, ~( H' q" a' I8 q$ u }
$ B- y( t! z7 p. \ else{
$ w4 Q8 K& a. m$ R0 V, a( b8 p4 a I = 0;- }8 @8 R7 u ^; a- m* E4 C: f& q
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?- ^! F# T7 c+ n4 ~
初学者,还请见谅! |
|