|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
u! y' n# k$ J* ^' R" a1 Ppublic void spreadMoney(int money){' G% `# V5 n. A1 I, ^7 H
// Randomly place money in moneySpace
4 }; x( ]* [% l$ H for(int i = 0; i < money; i++){
" s1 z/ X& z1 [/ h5 B% s9 a; t) l5 E7 m& S9 g
// Choose coordinates# r5 o4 ^/ y$ v9 z
int x = (int)(Math.random()*(moneySpace.getSizeX()));: o' b; @% X/ [+ e* q
int y = (int)(Math.random()*(moneySpace.getSizeY()));
. S) e* ?; F0 M# H2 e% ~
( k0 s" ^: B" @4 L // Get the value of the object at those coordinates% k. \9 y* k3 Z4 a0 @5 H
int I;
$ X. z; S* x, j$ ?4 t6 g$ ` if(moneySpace.getObjectAt(x,y)!= null){& `7 M" `6 }: b2 z8 _
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
! a( k9 H" x! N3 [2 E1 c1 \1 h' |: f6 a }
0 W; j3 a" J5 Y6 c; ] else{
# r3 b- m2 ?8 l4 ? I7 B; e& G/ S/ D I = 0;
" ~( r: ~8 ~. I( l' ] }
; L5 c% h5 i* w) U // Replace the Integer object with another one with the new value" n4 ^1 R6 h9 q0 k
moneySpace.putObjectAt(x,y,new Integer(I + 1));7 E, [( q) _1 i( l
}
/ S$ `0 A* ?' n$ o+ W: q6 L这里面if(moneySpace.getObjectAt(x,y)!= null){
- I0 `0 U4 j# k I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
) N% i$ L! h0 D! p! ~* M }
! e/ n4 w5 l: d else{
' o1 N& ]; q, W& o: A( g# j I = 0;- k6 i: u! q* w$ |
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
4 n; [% Y9 |. u) x$ |% N; P1 d' D% l初学者,还请见谅! |
|