|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
+ N7 c" M1 O( d2 Vpublic void spreadMoney(int money){' c! D4 P9 J$ P% T' g; f& H
// Randomly place money in moneySpace5 L( F5 p. Q7 X. N: s
for(int i = 0; i < money; i++){
" z% E' _* Z. B- t' [2 v. W" ?; N) S( U
// Choose coordinates1 Z. m) z- U& t6 Q7 p0 G
int x = (int)(Math.random()*(moneySpace.getSizeX()));
9 ~$ N8 M$ `) { int y = (int)(Math.random()*(moneySpace.getSizeY()));/ o `0 r" i$ }+ p
7 R& q8 B* A/ R) ] // Get the value of the object at those coordinates
# m1 U0 k; Q8 F int I;
" Y; X5 y1 `2 i8 E3 J/ A/ z; V+ e" J if(moneySpace.getObjectAt(x,y)!= null){" w' L+ n& Z+ g% J4 I% J
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();1 }9 ^ ~5 h3 H! d" b& a0 U' a! n
}
! _& V# P/ g) V r/ G: p else{# E3 j0 P7 x3 K+ V, y( Y+ r4 s
I = 0;* N; K" z) ^+ _. [8 `2 M
}
4 i1 p6 h$ ?$ }" ^8 K- T0 y // Replace the Integer object with another one with the new value
5 D: Y- k2 U+ M% z8 b8 u {8 L moneySpace.putObjectAt(x,y,new Integer(I + 1));
8 Y; C# U: n8 t, ~5 v }
! I) ?- b, n d" T9 O这里面if(moneySpace.getObjectAt(x,y)!= null){
! [5 I8 X: \; h) e I = ((Integer)moneySpace.getObjectAt(x,y)).intValue(); l9 Z0 }0 T/ l0 G2 B' |3 ^5 f
}* Z: A3 J) |$ D! B* {0 n2 I
else{+ d" h9 a! k: W" Y# C# v6 V) g
I = 0;1 w( l3 Q7 G4 j' S. ]. X
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
4 B4 l- X; U3 ~( H初学者,还请见谅! |
|