|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中" J/ a; I5 r; C. {2 E+ v7 ~
public void spreadMoney(int money){
n( v! f: y" N! }3 G // Randomly place money in moneySpace o- l; ^! M, {8 ]
for(int i = 0; i < money; i++){! Q) D4 a# N$ w1 I/ ~8 r
, v: {9 y" T3 F; Y
// Choose coordinates
# K$ B7 I" ~. _! k2 F9 ^3 [$ P int x = (int)(Math.random()*(moneySpace.getSizeX()));
. v7 K' J2 D' M0 V int y = (int)(Math.random()*(moneySpace.getSizeY()));
7 L2 c( ~$ F4 {/ f: V
) i8 E! Q7 D/ n9 S# [0 R1 x9 R // Get the value of the object at those coordinates
) T9 i3 j9 r' @0 \/ f8 t int I;! j0 z9 C* p T& {
if(moneySpace.getObjectAt(x,y)!= null){
: q, N1 A2 I) N1 H; q$ r7 N# j9 Y I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
" K3 y, L6 V0 N }
- C" |; a+ v# x$ {! `8 O; v else{4 A/ Q0 N7 m4 y
I = 0;8 \2 L1 ^9 R5 I/ N* t7 |
}+ x, k3 d5 i( F; M3 X( p
// Replace the Integer object with another one with the new value
! C6 \1 i( k2 ^, m; U' w' y' y3 w moneySpace.putObjectAt(x,y,new Integer(I + 1));
9 h6 O2 a* s* ?6 x& E }' I6 h0 V# d, ^2 G- |5 e
这里面if(moneySpace.getObjectAt(x,y)!= null){7 S/ G* ]4 K' d8 @1 Y7 s
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();: G ]( J* D% i6 z5 R P
}5 { |8 Y6 t$ n
else{
: t4 v8 z: E h" L I = 0;0 \. i+ K3 D4 m* r
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?4 ~) m7 J4 l' W! ~# {. d: Y
初学者,还请见谅! |
|