|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中6 u' W# ]$ B- ?/ U4 g
public void spreadMoney(int money){$ p \; q5 M J3 ~$ z
// Randomly place money in moneySpace
, E; ~. I5 _) l- C' @ for(int i = 0; i < money; i++){# v% r5 c7 }1 @$ Y" |" t
, g k( r6 ?* s; |& |" w/ x // Choose coordinates
% M" I# K8 A* Q int x = (int)(Math.random()*(moneySpace.getSizeX()));: @$ `3 g" ?6 J* L/ f) f
int y = (int)(Math.random()*(moneySpace.getSizeY()));
4 {. z* l* r2 b" i( A1 b
4 z- ~ s" S$ h y6 y; O, T // Get the value of the object at those coordinates
5 q* W, X# ^1 z, G int I;
) O( a* J6 I9 v5 `. B& n( X+ Z4 G if(moneySpace.getObjectAt(x,y)!= null){; G. |- N# C) l& o1 F* a5 x
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
6 }* g0 e7 O8 z c }
$ i O' c* t7 D, U. | else{
A8 W5 t/ q0 ?5 P& { I = 0;
9 g) V, v' ?( S J }- b+ ?4 h/ `4 m( [, X5 S3 Z4 f7 U
// Replace the Integer object with another one with the new value& b1 f1 w- v3 j9 Z$ V, |
moneySpace.putObjectAt(x,y,new Integer(I + 1));( i1 l5 Q% K2 X
}
) g, p+ ]* G. o; k4 }6 s这里面if(moneySpace.getObjectAt(x,y)!= null){ w2 H+ x" d/ F" q
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();2 M2 u0 z0 W4 R+ A
}
7 h# y2 ~; u' q* Q5 W else{
- Q' P( _: k( }8 _; @ I = 0;
0 L2 u9 E9 W: x5 I; |) H9 e是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
H4 Q' y2 ~& K; U初学者,还请见谅! |
|