|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中1 F1 i: I ?0 r6 Q9 u2 t
public void spreadMoney(int money){
2 J7 m. W* V5 @ // Randomly place money in moneySpace
\7 W3 B9 d6 Y% m for(int i = 0; i < money; i++){
: }7 d( A" j; j$ w+ W8 _
( ?- e* A' d v) W+ K" X // Choose coordinates% F6 B/ f' L |
int x = (int)(Math.random()*(moneySpace.getSizeX()));- p0 Q _. K2 L/ d t! j ]& `
int y = (int)(Math.random()*(moneySpace.getSizeY()));
. |6 t1 R" r$ R' k& t- U/ ?4 o1 {6 S# Z* ], |
// Get the value of the object at those coordinates
; C; J7 M6 y6 b4 A2 H* b int I;
/ [; A7 K& b7 I if(moneySpace.getObjectAt(x,y)!= null){
, d4 @, g0 H4 }; g I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();1 Q0 q6 }4 j8 I1 W$ G3 n
}% F. H. e- W+ w
else{
& b4 U7 g. C8 j% b- e9 |3 x# p I = 0;
: Z* r% ?/ ^4 K. @1 C* G5 K5 R }8 k$ e' e+ H7 C2 I
// Replace the Integer object with another one with the new value
' Z, J) i u8 Q8 M" } moneySpace.putObjectAt(x,y,new Integer(I + 1));
7 a8 z: X$ J" W' x }$ b: S0 s0 {- l
这里面if(moneySpace.getObjectAt(x,y)!= null){
5 `0 v& I6 l! A6 f$ p2 u I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();+ b \2 Y" [. J4 \0 Z4 Q3 _
}, L& L! o; f7 w1 j& r$ g, r
else{
. t0 s8 u7 j; p0 J) c/ i I = 0;) E4 V# L% }8 a: |
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
: W8 N0 _) s' c: F3 | H6 `6 \初学者,还请见谅! |
|