|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
3 F" @( P" R4 K! kpublic void spreadMoney(int money){
, G: c7 Y; E/ y) X+ l5 n! z // Randomly place money in moneySpace
( n( m4 d, ~1 m7 y for(int i = 0; i < money; i++){
0 G' Q3 s5 I/ P1 v8 p- C9 `$ q# W
// Choose coordinates* m7 R$ b0 e/ ]+ r1 x( s
int x = (int)(Math.random()*(moneySpace.getSizeX()));
a/ f8 x7 ]% M E! G" L: \5 S int y = (int)(Math.random()*(moneySpace.getSizeY()));! l' y$ n4 e. n, l
. {* m n/ { g* Z1 {7 g9 V
// Get the value of the object at those coordinates+ v2 Z+ ?' f0 D1 K# i
int I;
" t, g& g, H7 _8 I! z if(moneySpace.getObjectAt(x,y)!= null){
$ G7 |5 L9 c7 P9 B8 c. L+ h I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
/ k3 S! H4 D4 W8 I }
/ r- F3 b! e2 I5 q3 f1 c else{( F+ D3 P- M- ^
I = 0;
9 b; f( S* N% [; Q }
7 d/ F3 T6 ?* t/ Z // Replace the Integer object with another one with the new value, h( H% y t! g1 H1 @% ]+ Z
moneySpace.putObjectAt(x,y,new Integer(I + 1));
' ]: n% x( k2 z; D6 k, X } l% D( A+ B8 K8 B* k, S
这里面if(moneySpace.getObjectAt(x,y)!= null){5 }9 w/ v0 C9 Z! c4 z
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
1 z1 z7 v/ g) r. c0 }1 R }
* o+ R7 W5 Z7 L6 W2 d" x4 g. K9 _/ X else{% M2 f: ]1 ]5 z
I = 0;
1 I' b% b/ H& ?$ R是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?+ `* ~$ l7 O6 {" c5 C4 q. Q
初学者,还请见谅! |
|