|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
% l' u6 o% x$ O$ N& mpublic void spreadMoney(int money){
0 V9 J l, y8 a( C# c8 n // Randomly place money in moneySpace# z2 i5 q: r( M% W+ w9 `
for(int i = 0; i < money; i++){
# m) ^* G/ U1 h/ g U# y8 P& H( I# z7 ^4 D
// Choose coordinates* ^5 K6 T0 V5 e* X' o# D
int x = (int)(Math.random()*(moneySpace.getSizeX()));; d3 n0 d* [! r* Q5 e
int y = (int)(Math.random()*(moneySpace.getSizeY()));
) Y2 p! A$ p- \3 U
! [4 y( T" L/ p // Get the value of the object at those coordinates6 C" S; _4 i0 n4 x" L- @9 m
int I;- V- S3 a/ {% P1 S' y$ ^. V
if(moneySpace.getObjectAt(x,y)!= null){
3 _2 i% b- G/ _' B- j I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
# _& z- @6 z! _0 c( m }
+ L; E9 \" [5 {- }, P. [8 [ else{
9 g) A: M5 B/ X8 L I = 0;+ z+ c# I: t7 B! m$ h8 b' T
}
8 `/ n- _9 V H // Replace the Integer object with another one with the new value
9 d+ U9 W h) d: o% P; E moneySpace.putObjectAt(x,y,new Integer(I + 1));2 x, @. K% d Y! W, S
}6 G% Z. `5 G9 w
这里面if(moneySpace.getObjectAt(x,y)!= null){9 ?, P; S' T! V: [* J) l5 W s
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
U9 K: V6 s. s0 X L! f }* ^8 Y8 \. o0 O z o: O
else{
( a1 z, A7 Q4 ]& { M, x2 |9 W I = 0;8 v8 A: z- d& `* F# f) U! m
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?% `6 Q' z& ^% K% P# j5 x
初学者,还请见谅! |
|