|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
; A( F T: ]+ n( l8 s3 dpublic void spreadMoney(int money){
8 p$ f1 }' j' ]* U // Randomly place money in moneySpace
* }; @, o* W0 g5 X) J" a for(int i = 0; i < money; i++){
5 H$ @9 [+ `$ V! P8 f
; P$ S5 A! v, _; f& {% ~7 E // Choose coordinates
+ Y$ D1 j4 P& k* s, L) h+ E int x = (int)(Math.random()*(moneySpace.getSizeX()));
1 D1 }: O; P7 k$ d, S int y = (int)(Math.random()*(moneySpace.getSizeY()));) e6 E: w6 T8 J- g% l8 @( D$ A
s6 E# G& u+ G
// Get the value of the object at those coordinates
2 h1 |+ D8 u, ?1 V. L$ j int I;
* K! Z" I: {- U( u* | if(moneySpace.getObjectAt(x,y)!= null){1 d! G% S3 Y) o4 `7 ?+ b
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
2 x- q2 p7 W0 [9 k; Z }
2 N+ e W) T; R* x else{) y6 ^/ T" J. F% Y4 V* B
I = 0;
0 B/ G! g) T8 T" X6 E6 S }6 A0 S) I: \$ C+ W, L9 i
// Replace the Integer object with another one with the new value2 Y: y3 d& u& }6 e' I, X2 D3 i4 Q( X
moneySpace.putObjectAt(x,y,new Integer(I + 1));. c, W7 u6 {2 K! T7 j( m' }3 O+ Q! a
}
# s0 {) v3 O9 b这里面if(moneySpace.getObjectAt(x,y)!= null){$ O( y3 t& J+ g7 `: f. m
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
6 ~/ f$ j4 _5 f# i2 y }8 q1 B6 O A( f7 F
else{% _& a/ c* _9 z% ~9 c" ?
I = 0;( t9 ~, D5 z7 R
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
3 E1 m/ U8 P, _9 y! g" I. \初学者,还请见谅! |
|