|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
% h d Z, H1 J% Z5 {3 opublic void spreadMoney(int money){
6 g) C. B+ c' P1 C* `$ R // Randomly place money in moneySpace
* }; B0 i m5 B, e. T3 n for(int i = 0; i < money; i++){
, }7 v; a9 t1 A7 i9 P
& C3 @" S% x; b0 C: | // Choose coordinates
: N* @1 ], a* K! r( r8 l int x = (int)(Math.random()*(moneySpace.getSizeX()));: l1 U/ M: W. }" l: s1 Z) ]
int y = (int)(Math.random()*(moneySpace.getSizeY()));% z! F% Z5 _! n. a0 x1 o0 }. X
# r1 o6 w- n3 Z // Get the value of the object at those coordinates
) i" @$ E& l% y \ int I;
" P) C _/ E# l3 M$ V L% V if(moneySpace.getObjectAt(x,y)!= null){
; v+ H% ~, I+ i9 }- ?, M I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
7 Z5 j7 q# r1 B+ X, N! B5 h! \. F: w$ u }( R: c2 G$ X0 H$ A. v# b
else{; R# O: d. X; x2 h
I = 0;
, p$ N" m5 D7 |& a: D0 W }
8 P0 @% W1 ~$ G6 \. G // Replace the Integer object with another one with the new value @" A& A5 m1 H, A
moneySpace.putObjectAt(x,y,new Integer(I + 1));
8 L' B- y% K7 n' F% `5 E; o* ` }
% u) c2 ]' }( ~3 Y- A9 p# @, d这里面if(moneySpace.getObjectAt(x,y)!= null){$ r) l0 w; f: ?
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();( n0 S! z" u3 {& Z" B: L& ?
}
5 a+ k& u- i. q# @8 d else{& h' A$ W/ Z- x9 K2 P/ T9 U6 x
I = 0;6 a# V% v. [5 d+ x- l
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?, T& y& j/ p) ~/ p4 z8 L) B, o
初学者,还请见谅! |
|