|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
& ^. ~3 `" d" Y, I7 {& Q" \public void spreadMoney(int money){
4 o- Y% p$ ~8 L/ S( d0 O2 h$ ? // Randomly place money in moneySpace5 @9 A7 d8 I0 ` l! G
for(int i = 0; i < money; i++){
5 p& R* b. T% e9 J" S
* a9 d5 u" }* z // Choose coordinates/ \0 @4 }( U5 K% i1 w6 z8 w
int x = (int)(Math.random()*(moneySpace.getSizeX()));
# D0 f& ^/ v6 r, ?/ h int y = (int)(Math.random()*(moneySpace.getSizeY()));
, \* I8 T1 ^2 w% Y7 j0 Z3 @; V5 D& K l8 t( B6 X
// Get the value of the object at those coordinates
5 a( s- V |! N5 U! m1 A int I;$ H2 P" H1 n- w3 A0 `1 X! |
if(moneySpace.getObjectAt(x,y)!= null){
7 n {% Q, M! h& Z I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();9 _4 _& }* k N1 v. P7 E% B; }9 |
}
9 Z7 [4 _* H A else{
2 f% k p$ x! j1 z I = 0;
4 J9 p$ f# g. F4 `4 |$ H! D }; H0 H) R+ O7 ? T; L, m+ o
// Replace the Integer object with another one with the new value
r: D8 [$ `2 q" `9 A7 W moneySpace.putObjectAt(x,y,new Integer(I + 1));
3 [! F3 n F! f: k8 I }/ ~# m7 K2 v) T- z* w8 i9 M
这里面if(moneySpace.getObjectAt(x,y)!= null){4 u5 t( Y: D8 Z* C4 `
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();( _, \5 G1 D/ _) l
}
# ~& k3 S+ w8 G7 E/ ~) q else{
2 q( b4 q# G4 ^0 L8 V I = 0;
" M: O8 v) a9 V是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?: z. r2 C4 e0 }( A& e
初学者,还请见谅! |
|