|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中- o6 ?, y+ g/ i* g
public void spreadMoney(int money){2 P: t# C4 \6 G
// Randomly place money in moneySpace5 t6 F4 R* E- A+ l3 w0 K
for(int i = 0; i < money; i++){4 Y- A4 e2 g/ c, d
: x" m) C5 Z9 N4 M; k6 W // Choose coordinates& U+ a& S+ p$ F+ u" a1 y* o
int x = (int)(Math.random()*(moneySpace.getSizeX()));! t) w- w8 X7 t2 S) S+ {2 R! i
int y = (int)(Math.random()*(moneySpace.getSizeY()));5 F; j# A7 n5 E _8 ~6 Y
; z+ s. Q6 ^5 ^. S6 D3 j* { // Get the value of the object at those coordinates
% M6 W( @+ L) R6 z8 K int I;
a( z1 i) ^; k2 p; ]5 J if(moneySpace.getObjectAt(x,y)!= null){
% R9 w: g6 G# r# W$ w: s+ Y) X* G I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();. ~( `. Z- d5 ~4 l8 i) p4 Q
}: p) P( G$ i6 y# d0 P
else{% M h- s; w: v9 j$ H- d
I = 0;; w% I5 O; K( n' T4 Q" T+ v
}- t% Z! _& I, t4 l4 B% Z' X
// Replace the Integer object with another one with the new value1 }8 @9 L. W: ^8 M N7 e* ]. m) g
moneySpace.putObjectAt(x,y,new Integer(I + 1));
N( ?7 y- T, o- S5 ]( z }% W3 F' D9 Y& X. [
这里面if(moneySpace.getObjectAt(x,y)!= null){& ~* N/ z7 f7 B# ]* e7 Y
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();9 j) h( |2 T6 A" q, l2 ?8 {: z
}. L6 Y! Q( l* H+ t/ g k* r. j; Z7 Z. r
else{' j, E! o# A3 ?# n( F c4 G
I = 0;
8 u8 J% ^6 @3 N' |$ k4 O+ q是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
0 @* c. E2 ]6 k/ W初学者,还请见谅! |
|