|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
) K) n% \. H1 h; {. o T8 b" ypublic void spreadMoney(int money){
) L) H* o( O0 W: O! d // Randomly place money in moneySpace; J! ~! e$ x T. l( ]
for(int i = 0; i < money; i++){
5 T* I& S' k0 V7 M0 _/ C2 Y' S: r
// Choose coordinates' L. y7 }! L% f: x5 B- C/ V
int x = (int)(Math.random()*(moneySpace.getSizeX()));
$ ~' |4 f/ | X$ z) u+ g: r3 U int y = (int)(Math.random()*(moneySpace.getSizeY()));3 {& v5 b% @( |% o
/ \+ ]9 a B8 S' u- I& R) V
// Get the value of the object at those coordinates2 U7 k$ W' E; M h8 I5 w$ J
int I;. W) J( u* V e% q
if(moneySpace.getObjectAt(x,y)!= null){
' k t; K+ M6 _; s' g6 x% H3 Y: ^) F I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
3 Q8 v/ S* E, |7 l9 H' O }& d# ?9 j4 _ z# W
else{% f8 F3 B7 w5 i" y
I = 0;
/ n9 H/ Q7 t0 ?, j# ] }4 |4 _2 G+ M& D: L" b% A, m
// Replace the Integer object with another one with the new value
4 _) a/ R9 h) F4 l# J2 b moneySpace.putObjectAt(x,y,new Integer(I + 1));
: p6 i$ F$ \# ^2 g" O }
. l0 R9 z8 _" d这里面if(moneySpace.getObjectAt(x,y)!= null){
( H% Y# t/ b: r* n0 } I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();, G7 n( x" O3 ^/ w' O: J
}
! l' S7 q, A* }, s else{$ p0 l9 V4 j! u. K2 i! }6 @
I = 0;
' y V' r0 u: C6 {是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
/ G( o' F! l" E" E- y% {初学者,还请见谅! |
|