|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中3 \. l1 G% n4 D% F
public void spreadMoney(int money){, _9 f6 B9 h% D9 v1 P2 `: a6 X
// Randomly place money in moneySpace1 @ c" J" r. c3 g, d2 B4 {8 j
for(int i = 0; i < money; i++){
6 G$ E6 \* O3 M9 k1 z
' R8 d% v* ^9 j# R* b4 S- h // Choose coordinates
' m1 M/ m0 h; o. p int x = (int)(Math.random()*(moneySpace.getSizeX()));
$ _4 p1 s) F5 H, _7 a6 K& s7 u int y = (int)(Math.random()*(moneySpace.getSizeY()));/ g9 V$ D) a4 q# q9 h( t
, b2 `- A4 R3 @) {7 Y // Get the value of the object at those coordinates. [' I8 U0 f( a3 @& }% @
int I;
1 x* ^+ f+ G! C2 T4 }( [ if(moneySpace.getObjectAt(x,y)!= null){7 l a" L# Y% W Y, C6 W0 i* j
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();: _4 i) U' W& J0 S0 U
}. h' Z7 u& w8 d& P/ B- ^+ k
else{
$ L: U' x+ _4 [4 d1 f+ j' `- T& p I = 0;
& W0 j! |" Z4 y3 C' v# A }
; B# y4 |2 g# ^- }+ | E- m // Replace the Integer object with another one with the new value
8 K% {, g' g7 z- z moneySpace.putObjectAt(x,y,new Integer(I + 1));) K6 a& Z/ O9 r6 C. s8 `7 A
}
3 h" j+ E: S5 _: `, }这里面if(moneySpace.getObjectAt(x,y)!= null){: _6 h- T* r/ n: g$ F' }
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
4 S# P; r# d) s! h* @ }
& q; P* s) X4 y: a else{
" B: a1 N; l8 W' L I = 0;
7 H4 q. \" F6 @5 I是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
$ ^0 _( L! E7 r# R) I. [( b( `3 k, g初学者,还请见谅! |
|