|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
1 }/ t& r# s' ~7 T- xpublic void spreadMoney(int money){
7 N: r8 K' A2 [ // Randomly place money in moneySpace
; p: R; U( J+ i; \ for(int i = 0; i < money; i++){' U: P3 o8 ]4 Z! z: R
+ s0 i$ \" E; t7 S! X // Choose coordinates
2 T, b: V- H3 K. t+ u; O2 {; I int x = (int)(Math.random()*(moneySpace.getSizeX()));' h* j/ [ \1 K* n* W% G
int y = (int)(Math.random()*(moneySpace.getSizeY()));
# Q% ]* b" `) S( `7 X, a
+ Z: O% t6 a9 C! g // Get the value of the object at those coordinates
% m5 W2 L- M7 B5 `) i* }* b int I;
8 |% f6 [) h# D) H if(moneySpace.getObjectAt(x,y)!= null){: g* a) U/ I, t1 u
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();; h" v1 ~+ Q a8 n6 d3 {3 ]0 s
}
4 t2 `# A7 i, f- Q/ X/ ~: x else{: s; T& h( k8 H+ g0 j" ]5 {2 u! d
I = 0;
; c0 O0 N1 f- L+ d; G& ? }5 s' F* M, H' C5 X! K' W
// Replace the Integer object with another one with the new value a/ h( K3 x+ Y
moneySpace.putObjectAt(x,y,new Integer(I + 1));
, p2 n' S2 m* ]0 } D }
& `! \2 R, U8 j, |这里面if(moneySpace.getObjectAt(x,y)!= null){
: G: U$ M8 }4 y2 G0 T I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
3 F( {7 m- g* C$ g' V& C3 _4 j) q }5 V5 Q8 G* O2 L/ |0 y) \
else{2 E9 T8 I6 l- i) ^% t+ e
I = 0;+ V6 q! r; j; u# f. f) u! \
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
U! r7 |( d( D6 c/ Q" t! p: _2 X初学者,还请见谅! |
|