|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中7 s( t1 `" @& Z1 F
public void spreadMoney(int money){2 q3 w( B) @- m
// Randomly place money in moneySpace
O% j1 \- w1 U0 d5 G for(int i = 0; i < money; i++){
& ]+ [5 h( a0 }2 h) }7 p, `
+ i' b& p3 X; m, C // Choose coordinates
( H* Y" F& \0 S8 o% S int x = (int)(Math.random()*(moneySpace.getSizeX()));
: a, d' A( P# z- f1 }6 e int y = (int)(Math.random()*(moneySpace.getSizeY()));( }) d% o8 M0 M+ R) [& X+ P
( m) e/ S, R3 A# H4 M
// Get the value of the object at those coordinates
) P; z. w+ x, @$ ` int I;
* @4 q# a! U9 q* P/ ? if(moneySpace.getObjectAt(x,y)!= null){/ ?. `! l( a0 D3 w: Y
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();# |; C$ v: w8 }7 a9 s
}# ?* M4 W( i" }$ O8 [' B& ?3 w3 j
else{0 |# {* _8 S) h$ B% j" }5 b
I = 0;* ^( M6 t0 u0 O; g
}7 S& l1 q6 A. P- P$ q
// Replace the Integer object with another one with the new value4 ]. Q% }2 A+ B. a
moneySpace.putObjectAt(x,y,new Integer(I + 1));
, H8 D' a8 [4 T& G/ [ }9 o) q: ] N8 r+ |( `: F: K" |* s& E C
这里面if(moneySpace.getObjectAt(x,y)!= null){
s4 A9 f' H- y8 z$ I5 U I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
& v- F- u6 A: Z4 X }
5 j0 g- O0 j! m c% F6 E9 x4 t/ O else{
6 b5 H% K# d! ?/ p I = 0;
+ {% a$ y$ Z! p d7 r是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?0 S/ ?% F% ], B$ }) ]
初学者,还请见谅! |
|