|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
+ h0 E. B3 i, w9 ipublic void spreadMoney(int money){
* U5 t* Q. g& ~# d# C) w; Z // Randomly place money in moneySpace- }6 W5 `# E4 \
for(int i = 0; i < money; i++){1 _9 A$ }+ l9 K6 Z) t
5 ? A6 m9 T# K# O: v1 ~ // Choose coordinates1 ?+ Z* w0 X e4 G; ^! y6 P% w
int x = (int)(Math.random()*(moneySpace.getSizeX()));$ S, |& ~9 Y2 w/ c9 R
int y = (int)(Math.random()*(moneySpace.getSizeY()));/ ]1 _, [! _, L( W6 Z; H1 `( e
; U5 M# e: j4 n: @ // Get the value of the object at those coordinates& L+ B1 |) ]) c6 H
int I;+ C; n3 h$ c- W! O! [
if(moneySpace.getObjectAt(x,y)!= null){0 y0 v4 [6 }% w# q
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();: ?- b; _" J1 f8 ~0 D' |0 b
}
! G# Z+ B8 ]) q' B9 q else{2 F$ _6 a: _. ]- L" L. p: _; x
I = 0;
' N7 k8 B! }% |1 u$ T }
& j/ ^( n5 I2 D: Z* [/ x" i // Replace the Integer object with another one with the new value4 ]# I4 @% G4 p8 e/ q
moneySpace.putObjectAt(x,y,new Integer(I + 1));8 I$ n4 q) p! r8 _: N, ~' z! P9 _
}# ~3 p3 d* i) t! C! [. U! W2 X: i
这里面if(moneySpace.getObjectAt(x,y)!= null){
/ h- F n. y1 x& n2 n I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();) y3 y- h& }: @' V |/ F
}3 [1 _) F! G; _1 i4 m
else{5 A g2 v3 }- S* H
I = 0;
6 h. s; o* }& ~4 L是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
7 s7 U1 Q1 b, \/ {6 ]8 F. i初学者,还请见谅! |
|