|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
6 X& }* E: ~6 ~; r0 U0 n* r7 p, K/ Wpublic void spreadMoney(int money){$ Y# V$ [1 {& l* {; P& j
// Randomly place money in moneySpace7 v- B/ s' t/ J1 z
for(int i = 0; i < money; i++){
s" ]! d6 y- K: G, m6 \( C; S& E
// Choose coordinates! S _4 A( ~5 Y
int x = (int)(Math.random()*(moneySpace.getSizeX()));9 j: ^* }! H- |' Q7 f
int y = (int)(Math.random()*(moneySpace.getSizeY()));* M( m8 h, x, S4 h8 P, `: k+ C4 a9 c/ D5 r
) d1 m2 L* a8 q4 \) _
// Get the value of the object at those coordinates9 z6 o Z$ O- z
int I;
" k, N! z$ c) c \( Z9 G7 v if(moneySpace.getObjectAt(x,y)!= null){2 a: {4 n3 c6 s4 I4 [! Q
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
; U8 C' S! S! z+ ]% D }
$ ~; C( {+ x8 _, ?+ M else{
+ [, N+ t. F# M T1 |8 F I = 0;
9 ~1 ^$ @: ?2 w( Y3 W( w. R }
' u+ t& s1 s) F' {* s+ z3 H // Replace the Integer object with another one with the new value
* n$ Q$ a9 u% e+ S+ U: ~ moneySpace.putObjectAt(x,y,new Integer(I + 1));5 L, M `2 C& j# n0 @
}
* q/ f9 d0 C: D% S" l% Q4 P这里面if(moneySpace.getObjectAt(x,y)!= null){, B, e4 [, S7 q) e' ]
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();2 j4 n5 u8 n& e5 k9 F# k/ K
}+ l6 S: D7 i8 N7 j# u
else{
6 Z0 Y' N# [ b: o/ ] I = 0;7 N+ {% E4 _ R
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
) Y' R2 h# e4 ~1 J6 s( S初学者,还请见谅! |
|