|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
" k g& p% v4 ~. {public void spreadMoney(int money){6 N1 c" f. A, S2 }5 ^" g; k z
// Randomly place money in moneySpace1 O& U2 X% }) Z9 |+ b8 U
for(int i = 0; i < money; i++){
3 t" }3 O l, [6 z! Z% c5 I7 ?* {
9 f2 K" ?; h0 H9 }; h5 T // Choose coordinates
5 k* }/ D6 `: f2 x int x = (int)(Math.random()*(moneySpace.getSizeX()));
; V5 [1 K* y, D J" t0 z/ v8 R int y = (int)(Math.random()*(moneySpace.getSizeY()));
; _- Q: I; S. o7 f( S- d/ H8 [1 X. ?* c
// Get the value of the object at those coordinates* K0 D; Z. R5 a! w; k* o
int I;
+ H1 N _+ U) h) Y if(moneySpace.getObjectAt(x,y)!= null){
+ S& O+ Q9 K# U9 \: U I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
! A& r/ C& m% d, T$ L+ ~7 z) u }. J5 }* S+ |& A. Y& T6 \3 R: f
else{3 N4 ]' E7 a. `7 n* M
I = 0;8 V7 F$ P6 X( X+ f
}6 u% F$ H2 E$ c1 }) _
// Replace the Integer object with another one with the new value/ m' ?( Y+ F: T3 w* X
moneySpace.putObjectAt(x,y,new Integer(I + 1));$ n0 C) W, t5 F7 H0 q) j3 I
}; A: q6 R. u! U" v# g
这里面if(moneySpace.getObjectAt(x,y)!= null){
1 o6 r' q, u4 \+ E I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();7 |9 ?* I& F0 L1 r: |& u
}
8 L$ { J9 m7 ~& R" l else{. v9 w6 B; I2 X+ T
I = 0;$ j" ~- ? G9 _8 K+ Y" l
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
' S) ^( y4 {4 X3 L9 f) E初学者,还请见谅! |
|