|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中$ }. Y+ V- t) `/ N$ C) t. m
public void spreadMoney(int money){. a7 N7 q( [( h3 p4 h/ |
// Randomly place money in moneySpace" s" l! C9 y1 X
for(int i = 0; i < money; i++){9 c9 c; \9 u8 L6 M0 v5 ^ [2 I
L+ |4 t v! X1 c8 S$ W
// Choose coordinates- A2 w. T* Q; S
int x = (int)(Math.random()*(moneySpace.getSizeX()));2 m ]( n) c, [# F0 G
int y = (int)(Math.random()*(moneySpace.getSizeY()));
9 O8 ]( `% m$ A3 y
v5 _, v7 u" H( M! i // Get the value of the object at those coordinates
6 h/ A5 _; s a8 l, p2 C. W int I;8 h" I0 H/ ]; g* f
if(moneySpace.getObjectAt(x,y)!= null){
/ }, q5 A3 ]( f* s I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();. l9 x4 J8 O' I! d5 O
}
, b/ a7 O( J) k+ v4 M else{. Y# i+ ~$ a, O
I = 0;9 A% V! K$ W/ A$ h. L
}! L: S" p% ^. R% N3 R& e: J
// Replace the Integer object with another one with the new value% `7 n9 n2 r) ~3 d! q: E
moneySpace.putObjectAt(x,y,new Integer(I + 1));6 J, j* \7 `4 N. n5 O% K+ g
}7 E' s( O. p6 _2 c
这里面if(moneySpace.getObjectAt(x,y)!= null){% s" c; _6 Z2 a6 m9 U
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();% z$ V* [! b4 i0 F; n5 i
}: z1 @/ s* ?: M& {+ i
else{4 n& x9 G/ e* v; e7 H3 W w
I = 0;/ p& E9 {# E% Y. q+ L. e
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
9 ^# j% ?, {! n: V- G9 Q6 Z$ ? b初学者,还请见谅! |
|