|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中1 F8 o8 o1 o9 F4 y) t; C
public void spreadMoney(int money){' f( g% \. X, [8 N
// Randomly place money in moneySpace
% c, `* a8 [/ q' o# G for(int i = 0; i < money; i++){
$ ~0 P) J0 v! |+ j7 l( z2 {3 G3 C
// Choose coordinates; ], i$ K j0 d" ?; I) z
int x = (int)(Math.random()*(moneySpace.getSizeX()));5 n+ W6 V; M/ u- R( @
int y = (int)(Math.random()*(moneySpace.getSizeY()));$ C% C# `0 |7 \/ K# N& Y* r
# {: e0 h- H/ l- S4 J
// Get the value of the object at those coordinates
% M: S$ v) `6 m9 d int I;! w" ~& x0 w% i3 d
if(moneySpace.getObjectAt(x,y)!= null){- X0 B/ M* o5 r$ l( i
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
3 P; \; R, D" W2 v. r; J }: q+ P) ~, I* d, `5 p2 u
else{5 V# |/ R! w8 v m' @. X
I = 0;9 k F- Q" }- A" u, V
}* ^7 q- [( o" v8 Z; A5 f
// Replace the Integer object with another one with the new value
. f6 i' E* z# e% H- T* ? moneySpace.putObjectAt(x,y,new Integer(I + 1));
* A, v3 f: w3 s( p }6 O2 c( `/ Y3 ^8 s& C
这里面if(moneySpace.getObjectAt(x,y)!= null){" H- F' l9 W7 x& [9 L( T7 T
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();, Y8 b3 L8 B4 j5 s% c- _
}1 [: w# J2 r. D3 \4 G: G
else{1 }- ]7 s/ _3 `5 ^" a
I = 0;
' Z) D- ~0 C/ L' d ~" u是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
, B y, Q' ~+ S5 e初学者,还请见谅! |
|