|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中$ Z( o" f% y% B1 D9 K
public void spreadMoney(int money){, @: ~. y2 o" H- q" @
// Randomly place money in moneySpace
9 o% z4 n/ A2 K# ]' @. f for(int i = 0; i < money; i++){, C- p9 @1 B! I% `- [3 S4 f z- H' G
2 h! F3 i. }# \6 `
// Choose coordinates$ \2 U7 d) K1 y# ]# d/ o
int x = (int)(Math.random()*(moneySpace.getSizeX()));& \/ j- `: @% d1 I7 ]2 m) q+ l& ^5 X
int y = (int)(Math.random()*(moneySpace.getSizeY()));
7 U9 J# m8 v c- f
' E8 v: P# ^, y4 E4 S8 `) t6 \% i% A // Get the value of the object at those coordinates
: V# N" Q! R2 l c8 Y0 e8 R. A int I;' Z. O# I8 [' Z4 K* z
if(moneySpace.getObjectAt(x,y)!= null){
+ X4 a) c( l# A6 r I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();7 A; s, H6 \6 T+ O7 d
}! ?+ c, S R O- W* f
else{0 F- s8 b7 e( z/ N% U3 w) e+ d2 v5 p
I = 0;8 {" l5 {/ h/ l: I
}
. {* P. u# S1 b& Z3 ` // Replace the Integer object with another one with the new value) Q; ` H, M& n
moneySpace.putObjectAt(x,y,new Integer(I + 1));+ X5 M, S4 x% K g
}
`5 W' N8 y) A0 r这里面if(moneySpace.getObjectAt(x,y)!= null){
( r( b/ s% L7 |. p3 `7 o) H I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
0 v. x! N7 ]' H& @ }
: J) ^: G# T% V6 D else{
' a& P9 i1 n& A, }$ f: L I = 0;! r5 ], N/ v6 ?3 ]# h0 K
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?9 c b: J2 U7 i, P7 x
初学者,还请见谅! |
|