|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中: m9 c1 A2 r K+ g) Y7 I4 {
public void spreadMoney(int money){
9 w% O. |' W9 ~ // Randomly place money in moneySpace
+ E g' k( a( |" j1 { for(int i = 0; i < money; i++){, j3 \* _1 J2 f6 D$ Y& \
! t, y9 o K2 R2 _6 T6 o' @+ ~ // Choose coordinates
. K- d* K/ ]* N# c M. {7 C int x = (int)(Math.random()*(moneySpace.getSizeX()));9 _& z2 a& B9 H: w" m3 N+ o, U' F
int y = (int)(Math.random()*(moneySpace.getSizeY()));) m* c5 s; u& D @) {
& t6 N& [, r7 f" V3 ~ // Get the value of the object at those coordinates! @/ R$ w) M" r/ a- w) F
int I;/ c6 I- d1 V; J- a5 N+ K2 R" @
if(moneySpace.getObjectAt(x,y)!= null){
2 u( \+ Y/ W, R2 ]1 J$ | I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
9 i; y |8 ?/ s0 A }. f2 B6 ?' d0 [. F k# ~
else{
' u+ a% a6 x( e+ m" K- f I = 0;
8 y6 q1 V- `7 `/ X }- r0 e7 U8 ^) S/ r
// Replace the Integer object with another one with the new value- a; B N5 G& E: a
moneySpace.putObjectAt(x,y,new Integer(I + 1));6 K+ ?: _4 r# }6 C; w' a
}7 }& H* @3 n/ z: m1 u0 A) b
这里面if(moneySpace.getObjectAt(x,y)!= null){7 m6 \ g% |: T/ B
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();' t9 G& Z6 _- M; g# [& T
}
0 W6 s: ?& M( J9 ]" U else{2 o: z" D/ K9 e5 o8 W, Z
I = 0;
) w- X/ F! C; g# L6 _$ _是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?7 r) P7 r8 j& H8 {; [
初学者,还请见谅! |
|