|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
- S! @& ^3 i2 k, C3 T6 Vpublic void spreadMoney(int money){
, g% |) z. v/ o$ R. |2 G // Randomly place money in moneySpace
0 \7 C3 G* c- @) V% e2 N for(int i = 0; i < money; i++){
3 T! u/ X- E6 M# U( M9 D% c5 L3 _; ~/ S. R" x' R/ f
// Choose coordinates3 v6 u0 b% I7 x; ~
int x = (int)(Math.random()*(moneySpace.getSizeX()));5 [0 z8 R0 }6 {. ]
int y = (int)(Math.random()*(moneySpace.getSizeY()));7 t0 }0 C5 m+ \* ?
; z. L8 m# o7 i+ q" J# J* u // Get the value of the object at those coordinates) P* z- H0 r7 n0 b4 q/ ?$ q+ T( |
int I;
, _& w1 s6 ~% l if(moneySpace.getObjectAt(x,y)!= null){
2 G5 `4 ]4 A. I4 f, ^ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();% _; D8 I+ g3 x& a9 ~
}
8 g* f6 m: [: N' J else{
% K$ E8 p0 F `( N I = 0;. [ ?" S1 v1 E4 r
}$ B3 T$ g/ ]3 m3 p4 X
// Replace the Integer object with another one with the new value, h7 c' P( x/ S7 ]
moneySpace.putObjectAt(x,y,new Integer(I + 1));; H: S7 }9 y' |+ O9 E4 g7 n; w
}
, g' R, L2 h. x/ X( }; u这里面if(moneySpace.getObjectAt(x,y)!= null){
/ j0 B- o9 O# c# _6 _4 ]* s8 S I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
! ^( J- Y: v* g' R; \" l- L% Z; } }
- z; @8 E* t. H2 w9 W else{3 B+ _- s2 g" t }- h
I = 0;/ o p4 T5 z, @$ o
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
- U {' y- G8 b+ I) \; J# b初学者,还请见谅! |
|