|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
' Y$ h$ B* t! ?% l' A. J: mpublic void spreadMoney(int money){2 k F8 I( p6 I4 I- q" M
// Randomly place money in moneySpace* N9 C0 r( ~" F1 Q
for(int i = 0; i < money; i++){. d- X8 {. F6 U X8 n; L9 i+ V# g' \* t
1 T6 i1 J) _- U6 f // Choose coordinates# c; _ b0 V' U( F; _" y
int x = (int)(Math.random()*(moneySpace.getSizeX()));: E9 q, v8 {* w7 B2 j) q) P
int y = (int)(Math.random()*(moneySpace.getSizeY())); \. [" c0 D; w/ X! t3 E ]* H
+ w9 O4 q3 P/ i4 w9 e // Get the value of the object at those coordinates4 U3 }" W9 u3 ?' D
int I;
- m2 ], {4 M) m( f' U% i$ S if(moneySpace.getObjectAt(x,y)!= null){0 `5 v! B# F9 T1 V7 X
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
; K7 E, G. C2 H* M+ @+ s/ { }
# w2 b2 W( V4 }9 } else{
8 d. A1 c: L( [ I = 0;
7 d- W, E0 \/ o% x5 G( c+ F X }
8 `0 o+ I$ t) a) X: k- k5 G // Replace the Integer object with another one with the new value) g, r; H* a$ a; J+ Z7 l h, D5 `
moneySpace.putObjectAt(x,y,new Integer(I + 1));3 z d6 D( T, Y4 H; K8 k! \
}
& `. G/ N7 L3 o这里面if(moneySpace.getObjectAt(x,y)!= null){$ A! Z9 I' h7 R4 j" f9 {
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();: R M! J% M1 c* O7 f2 X! |
}
6 L1 m7 J% r% [$ Q5 J8 B else{
x& w* F" x" R. c* F% s! z! o I = 0;
. {( [8 r4 f) v) B是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?$ ?5 z% F5 H8 G, [
初学者,还请见谅! |
|