|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
$ h) A d6 O/ ?* X1 qpublic void spreadMoney(int money){
. _, {8 d' C' m/ [% J // Randomly place money in moneySpace
8 p4 P' L1 @% n for(int i = 0; i < money; i++){
2 Q3 _& {. B. B! I3 Q L
- s" y3 I: W5 W3 V0 p' P, V9 N& U // Choose coordinates% g7 a; q" u6 P1 y, |2 P$ O
int x = (int)(Math.random()*(moneySpace.getSizeX()));
2 t( V& P8 D4 [: e! N( T, ~. H int y = (int)(Math.random()*(moneySpace.getSizeY()));
6 W3 P8 @5 I `/ `
% Q* o9 Q$ t3 f! n0 b W$ ? // Get the value of the object at those coordinates: a2 d3 q) R6 h% I' T" }. Q6 L
int I;
% [4 V, a3 B, K6 o, j if(moneySpace.getObjectAt(x,y)!= null){: M1 c' M8 Q8 j4 p
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
# b$ c% I% P* f, S }) |4 m( W7 P+ S1 W5 t2 s. H, `
else{
8 m2 s5 A8 ~# U5 w: I8 w2 { I = 0;
, O) l( F7 c! m% c6 t1 c5 X }
2 W# e. {1 X2 K1 {% y // Replace the Integer object with another one with the new value
3 e- f$ H4 X" y3 R/ \( s" }$ n moneySpace.putObjectAt(x,y,new Integer(I + 1));5 f. Q7 a: u8 E
}. Z1 [5 H: w' j C: p9 y2 {& \9 ?8 @
这里面if(moneySpace.getObjectAt(x,y)!= null){8 o" n) i: L+ n- j" }+ [" n
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();, ?: k S. W' u- z! x
}
# ]* v1 U6 n8 ?5 E$ g else{
9 B; S* c9 y C1 I I = 0; |/ Z( P1 i6 S+ u( u# m
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
1 X6 Q9 g4 Q# A4 _: @( D初学者,还请见谅! |
|