|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
: Y( E) X, [8 D" S: K6 tpublic void spreadMoney(int money){5 E2 ?8 I: A5 ^( K5 H! U
// Randomly place money in moneySpace4 M5 Z W3 U$ F9 G# c
for(int i = 0; i < money; i++){" `2 V3 f4 |; u- u: t5 `
* @, o s( Y1 g/ a
// Choose coordinates8 H1 f; x' V, F" u7 ^
int x = (int)(Math.random()*(moneySpace.getSizeX()));3 Z9 S* P# L* [( G
int y = (int)(Math.random()*(moneySpace.getSizeY()));) d3 `8 K$ X9 q
2 r- ?3 y, x5 c" t% k // Get the value of the object at those coordinates) A$ D! l+ N' I [
int I;( [( w& K2 E/ ^ I
if(moneySpace.getObjectAt(x,y)!= null){
; ?& o" A, M% [: F: D$ @ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
6 B2 G4 _4 o! L* R% u) r }
" F& s* V! I' t3 d+ F else{
5 U8 a: O0 m J0 K2 z k I = 0;0 n4 m9 B' Y) Q- R" ^3 Y' q6 ^
}
/ Q8 `- p7 ^1 A5 x4 b // Replace the Integer object with another one with the new value' a1 T+ f: u' S+ {4 O2 Q8 g* A
moneySpace.putObjectAt(x,y,new Integer(I + 1));
3 y G( m' M% K3 A! O- S }
2 t7 _, j9 ~2 D: g& W7 B* j这里面if(moneySpace.getObjectAt(x,y)!= null){
\8 m( e5 [! I( _ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
8 U. Q8 f! u& n! N& [9 f7 I. j# m* x }
4 [! [# q8 s6 O1 a ?5 N& i else{
; ~% v- |+ H2 d$ e I = 0;
; n1 f% V i6 r是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
9 t4 R0 R4 }$ E! K1 \初学者,还请见谅! |
|