|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
( c5 y. h: D$ x. D, h5 t# _public void spreadMoney(int money){- Q, N8 j6 g5 y5 J# [9 \+ q
// Randomly place money in moneySpace# b- f0 ?5 r1 t. P0 x3 u4 q( o
for(int i = 0; i < money; i++){9 h8 _) {3 D. x, h8 g
+ t6 R7 {& f+ X9 b' `5 L2 K$ I
// Choose coordinates8 D1 ~7 q& g6 `- R7 W, h+ n' n
int x = (int)(Math.random()*(moneySpace.getSizeX()));
" o# O! W" o& b int y = (int)(Math.random()*(moneySpace.getSizeY()));
3 R& D6 l+ d' Q; T( m7 T% O. b4 {
% X5 J$ @: G% O. ` // Get the value of the object at those coordinates
# f9 m6 P( @1 s" ]# m4 v" p int I;
8 p3 R6 A& T9 v! ~; ` if(moneySpace.getObjectAt(x,y)!= null){
& _4 W v8 |1 @' [8 a. e' y, X1 i I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
9 c; \& L6 a1 O }
1 @5 e6 ]3 {% @! W else{
' u! ]+ @% F( F1 i: S I = 0;
8 R* Q+ P: E( G }$ p4 f+ Q2 W& G) M
// Replace the Integer object with another one with the new value' ]# }* y* I' O/ n4 G. ?" a* q
moneySpace.putObjectAt(x,y,new Integer(I + 1));8 W) X4 {5 P; t4 L$ u6 R- Q
}
2 F$ \9 \( P: R& A1 T: i. L* u这里面if(moneySpace.getObjectAt(x,y)!= null){' s/ p- H2 K6 Q
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();9 e. K6 Y4 N, I/ g7 a
}
& ]1 W( v, b) }: _' A1 k8 V else{2 A* m D" ]# K# X/ u# T
I = 0;7 K0 W: ~! C& s# r: {1 C; ?
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
0 m0 n5 v1 f' u; k初学者,还请见谅! |
|