|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
$ R2 N* j6 B- n4 vpublic void spreadMoney(int money){- u6 Z0 h; v7 h' B1 m! i
// Randomly place money in moneySpace
7 t/ e k" K3 `, m for(int i = 0; i < money; i++){' e+ |# D8 a ]2 L
" m0 C O0 }, n- Q; q1 I. y4 ` // Choose coordinates. f) k. e2 O. b3 D c
int x = (int)(Math.random()*(moneySpace.getSizeX()));
+ ]2 k, `2 s2 P int y = (int)(Math.random()*(moneySpace.getSizeY()));) k0 g! y. C) F
6 C: p& I- a' B& G. r( Q // Get the value of the object at those coordinates* G1 u; c3 Z2 J4 k- k
int I;/ `) k l0 z+ F8 T' R9 c; F
if(moneySpace.getObjectAt(x,y)!= null){
. ^. d* ^% M, H I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
0 Z: B& p/ j( a+ F) x- Y }4 S9 D$ v8 a, K# O, l' Q$ N
else{
2 v: r. G+ O# N$ O# Z+ }$ i I = 0;
& ? ]: E$ `' t) U& z0 N }" z+ K- m) G3 i4 l& ?
// Replace the Integer object with another one with the new value* M9 S) u3 `" Y+ D2 X
moneySpace.putObjectAt(x,y,new Integer(I + 1));" Q4 e2 r+ x0 L( T6 v9 E
}
3 Z( x# l% t8 h; n这里面if(moneySpace.getObjectAt(x,y)!= null){
. y6 W0 p) H B I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();) r- j# q( O. X! s E
}% y) l# U" S. O) s* }/ s
else{& U; k7 w' Z- t. K4 x6 Q( H
I = 0;0 B3 x8 ?+ L1 S
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?! d0 w3 Y: q/ q* \. m
初学者,还请见谅! |
|