|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中( `/ ~' s( a- d. r {+ r
public void spreadMoney(int money){+ d: U+ S6 \4 V( V, N
// Randomly place money in moneySpace
* \ _- M% {; t# X2 { for(int i = 0; i < money; i++){
. v6 V+ |2 d* L3 Y
+ o/ ^' W2 r( D // Choose coordinates, {' F% @, v& v; `- `$ R
int x = (int)(Math.random()*(moneySpace.getSizeX()));
% y r- g6 [0 Z1 Y2 ]: d4 t int y = (int)(Math.random()*(moneySpace.getSizeY()));
( `% t1 T, {$ X
" H/ D! t! K% l- M% ?# Q: M7 D // Get the value of the object at those coordinates( S0 _! x( Z, p1 c5 d
int I;# {- ^1 t( h: p: G3 e
if(moneySpace.getObjectAt(x,y)!= null){; s( `; v: W8 t5 }1 X
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
1 M4 q- N! Y: O# `: v! N$ k5 K }
- C* K& h: |: G+ @& P1 l else{
% k: c3 _ ~) x: n* X5 y* P I = 0;
: B! V% d0 j) B0 C }
6 S x( {( M) a& X0 \, L // Replace the Integer object with another one with the new value6 m2 ~9 }% i7 s
moneySpace.putObjectAt(x,y,new Integer(I + 1));, J+ p& P! W4 R
}
4 q; z' l$ C5 G2 w8 K6 s这里面if(moneySpace.getObjectAt(x,y)!= null){
. w6 R8 Z1 i# P; e O8 M8 W$ }& U/ c I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
* M/ T! _0 Q: X }
% l4 L( f, r6 `5 B9 L else{- k$ i6 }6 l, ?; y1 H
I = 0;* ^5 l* P0 ^# F' a* @, |- t
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?+ d& z) k& j& M; a5 {2 f6 G% `
初学者,还请见谅! |
|