|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中9 G9 e% A, i8 _9 Q$ `
public void spreadMoney(int money){2 v' s g) x3 k$ ?0 y0 P; r( b: [
// Randomly place money in moneySpace
$ j5 q4 C6 Z V0 v) V for(int i = 0; i < money; i++){9 H$ q0 \' x, F* I( L' {) }- v
# r. X3 i9 Y7 J // Choose coordinates
. o, N' h7 v5 s. Y. `% S1 k int x = (int)(Math.random()*(moneySpace.getSizeX()));
6 l8 t, c/ R9 N6 d int y = (int)(Math.random()*(moneySpace.getSizeY()));8 g! {: m8 k0 D! R% f3 L. c- d# r
7 L' O5 ~ g( v( X" K5 `7 [ // Get the value of the object at those coordinates
+ z M8 J' X' Q/ F/ u* _6 Z+ P% ~ int I;
( N2 G6 K+ R; v; K" f6 E if(moneySpace.getObjectAt(x,y)!= null){
8 c$ C* O6 g( Y- A; _ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
0 a* ?$ v& Z7 f& E5 L+ Y' R }+ ]7 v# l* H" s% D3 H: @
else{1 q- M; j3 [, J, r0 ]1 {
I = 0;& r$ J' Q' X, k0 {; o
}
, ?# y+ D/ z' z" T; F0 A // Replace the Integer object with another one with the new value
8 r/ q/ y' E" D% m1 W moneySpace.putObjectAt(x,y,new Integer(I + 1)); L7 i1 d( b2 Z v2 R
}
3 ^9 l% W& Q- }$ b. p这里面if(moneySpace.getObjectAt(x,y)!= null){
" S: x4 f: @! ]+ z6 V6 n2 K8 C I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
- b5 T5 b+ [) h j: I% t# b+ X }
5 u) ]: v5 N% G8 K! A9 Y% Z else{
# Y/ c$ D0 g' T" R I = 0;/ U1 O4 d' q4 P. G( }
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
9 p, s: d+ |8 U1 ^' g初学者,还请见谅! |
|