|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中0 k7 \6 [# B9 B- F9 V
public void spreadMoney(int money){" y) {& T( E1 S) q/ R$ V3 _' u% h" x
// Randomly place money in moneySpace @. X/ u0 [7 [8 @# A2 L
for(int i = 0; i < money; i++){; B9 ~3 u o t: j
% g6 R$ `0 R/ ?: I; f9 n
// Choose coordinates2 H0 Q5 F4 p# x# ~& A
int x = (int)(Math.random()*(moneySpace.getSizeX()));/ ~+ Y4 w/ W3 ^* w$ X& v. r0 m9 l2 {
int y = (int)(Math.random()*(moneySpace.getSizeY()));6 L$ h4 z8 H/ V# l$ B) ^7 F
" }( `1 A! h" n0 H; Y
// Get the value of the object at those coordinates; H. J( g$ q" R" r
int I;
( f2 {/ X2 l. ?2 t0 T9 ^ if(moneySpace.getObjectAt(x,y)!= null){1 j4 d* N# }% G G8 R3 A
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
9 T) Z: q# N2 _) ~ }
5 ~- J0 h+ V3 l8 H8 M else{
. Z k4 [# p7 l5 S I = 0;& R# A3 w. [9 K
}; V* r8 [2 A, n, L. v0 b9 \. C2 S. Y
// Replace the Integer object with another one with the new value
4 l& u* r+ h& c: h0 [1 r: U moneySpace.putObjectAt(x,y,new Integer(I + 1));0 V7 j: f9 V6 v8 F) F9 n. w) b
}' \ z/ y5 A$ N5 ~
这里面if(moneySpace.getObjectAt(x,y)!= null){" k9 H C. }, o* ]8 T
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();( A+ Y% m7 i! e, ?
}
$ v, M( C2 D' v) d8 P else{5 A/ N, I) \* b* R$ k4 K" x
I = 0;+ l) w+ L9 V1 g) z
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?$ J* f9 s4 ~7 r; }7 j3 E: I* \
初学者,还请见谅! |
|