|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
; u& f" K8 P z9 `$ ^6 V Jpublic void spreadMoney(int money){
: ]( ?1 C' T7 ? r& n, u3 ^ // Randomly place money in moneySpace( a* |; D3 A+ I5 t# E) ?) ~
for(int i = 0; i < money; i++){
, ` d' o/ y; a- P) N4 c' a! c& z: C/ ]" h% |7 L
// Choose coordinates
' R3 B$ _* ~+ y9 L6 A$ @. V2 k int x = (int)(Math.random()*(moneySpace.getSizeX()));
, Q* Q3 H0 v8 f$ R! L3 } int y = (int)(Math.random()*(moneySpace.getSizeY()));
8 y; C9 J5 Y; A* c$ Z) }. x( c R1 ^' U* B, |- d
// Get the value of the object at those coordinates( N: l) z1 U G0 r
int I;
% A$ V, M! {# b% F' X if(moneySpace.getObjectAt(x,y)!= null){# n3 ]1 P8 K) h! U
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
$ x9 a6 Y. A | E }& s* C8 k# X h4 q4 j& A# m
else{; k& {, q/ A) W L. G. b6 m. ^
I = 0;
' V5 f- m8 D# ~- {" m }6 Y9 ?: J; B* i0 s* \$ c, ]
// Replace the Integer object with another one with the new value
2 C8 x# w2 q- h0 B2 N% B1 J# R moneySpace.putObjectAt(x,y,new Integer(I + 1));
2 N" B( z- M" ^! E }; t0 h8 z$ m+ e/ X9 b* Z/ Q [
这里面if(moneySpace.getObjectAt(x,y)!= null){6 q! U3 y6 i% e7 d
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();, j6 D( a( l7 C/ b6 K4 V
}
# } V' h) R) X else{
4 k4 m5 E6 O: d5 n3 l( @ I = 0;1 t' R0 N# [7 |$ [/ K6 M# b
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?. l8 N' f* H) k3 C1 R
初学者,还请见谅! |
|