|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
) V* ?9 ?' P& o9 @public void spreadMoney(int money){
* p9 W0 P% ^) E' e% ^ // Randomly place money in moneySpace& j$ Y. y( m7 p& ?
for(int i = 0; i < money; i++){
1 L. s: S* U9 H, F' U( J
( L! |3 F/ y8 R // Choose coordinates) W r+ \0 X7 A# c
int x = (int)(Math.random()*(moneySpace.getSizeX()));$ b4 {6 g; \) X7 ?4 o
int y = (int)(Math.random()*(moneySpace.getSizeY()));, q i1 m; D& v% D! D# n9 k: p6 J2 m
u3 A% H' t* Y, e // Get the value of the object at those coordinates w W& d0 O6 z9 ]' k/ T3 n% g+ A
int I;
8 D( y, X z3 r4 _4 w if(moneySpace.getObjectAt(x,y)!= null){2 z9 p! n4 K1 c$ z: ~
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();: Q `+ D, U1 F4 ]$ a
}
" P* J1 ?$ G; L2 z. I* M, D# Z! i else{6 Z* p s; L4 Q
I = 0;
7 H* N( B# H+ r0 L }2 k* \( H1 E* \( ]
// Replace the Integer object with another one with the new value, k& f3 X8 U1 V2 f3 k
moneySpace.putObjectAt(x,y,new Integer(I + 1));
" }/ h1 o, S4 v8 I, D }
0 A% H" ?. x5 s# P& I/ D这里面if(moneySpace.getObjectAt(x,y)!= null){
; k* G' |5 L* p0 T+ y I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
$ g. l1 z9 b* Y: W* A5 k }2 G1 A$ U1 P4 G8 ^: M
else{) G- V6 ^* [0 F: l8 f, s
I = 0;6 w2 Z0 U" [6 {' d4 b$ o. H+ b
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?( t- ?4 L2 @8 {, L- @% b
初学者,还请见谅! |
|