|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中; Y5 U; A# @1 ^) ?9 X
public void spreadMoney(int money){
- d& ]& {& i4 L. c! C // Randomly place money in moneySpace
' ^7 \6 I, h3 M7 E0 A for(int i = 0; i < money; i++){
6 [6 Y2 k E0 k9 s% x0 k7 _" x) d( ?/ n
; b$ n, Q) \8 o( ^ // Choose coordinates$ y; w, Y0 @) {: A* q
int x = (int)(Math.random()*(moneySpace.getSizeX()));4 x% z. C* f$ t' w, v
int y = (int)(Math.random()*(moneySpace.getSizeY()));0 D! W: k, ?; v8 h" v6 _. X
5 q( l8 v$ L, @9 P* A // Get the value of the object at those coordinates
4 A/ E V% Z) e+ I. A. v5 I int I; k+ ^- c o3 ^; f5 N
if(moneySpace.getObjectAt(x,y)!= null){- q( L9 d4 u3 P( H
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();( B8 t4 R6 S3 k% f6 A
}6 E9 J' E/ R4 h: q
else{/ {' Z2 q; S( n2 n4 h) n" `# }
I = 0;
\6 j1 H# {0 Q( [" [ x' v }/ k2 f+ P1 F) P$ K* N
// Replace the Integer object with another one with the new value
# v2 f9 K) H7 C& w J7 Y; r moneySpace.putObjectAt(x,y,new Integer(I + 1));
5 U, ~* g4 C, s; J8 Q } h& I; {& a! w- C8 N2 M, S
这里面if(moneySpace.getObjectAt(x,y)!= null){
* E/ j( J% [, {6 h$ g+ Z9 u' `! p I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
' |0 X& x4 `9 v }" K8 Q: v7 \0 M3 g5 v2 D
else{: S% S3 r' g; d- m( z
I = 0;' H& c4 v# O ]$ {
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?# L" p) G2 z0 t3 t! h( q
初学者,还请见谅! |
|