|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中3 A' X2 o! \ M) i- i
public void spreadMoney(int money){
' D( G+ f" |" c0 c) A# m% L$ {( i! k/ i$ V* Z // Randomly place money in moneySpace
, n, L! m2 H* H for(int i = 0; i < money; i++){
$ J" R- _3 q, A& j, M% p( h" j! k+ Y. |
// Choose coordinates M+ t+ C, f" I! ~2 J4 P) Q( V( E& F. I
int x = (int)(Math.random()*(moneySpace.getSizeX()));* e! p7 g+ \, G5 S- C
int y = (int)(Math.random()*(moneySpace.getSizeY()));
, X+ }3 Q. V7 w! K% ]
* y ~ z$ x/ C- W v // Get the value of the object at those coordinates8 A" ` z( [; W+ K
int I;
" `( M* a ?% a! d* X7 U if(moneySpace.getObjectAt(x,y)!= null){9 Q1 S) @/ U8 E" j7 N
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();. v5 b+ j- H6 ]: ?0 X8 Y
}* c! X3 U& T$ I1 \
else{
6 c {/ x8 G* _+ y( J2 B I = 0;
# z% z9 S$ a I# H# L( e2 K }
7 k0 E) b+ S& l // Replace the Integer object with another one with the new value
* O; {3 X& d9 K+ o9 L, N1 q moneySpace.putObjectAt(x,y,new Integer(I + 1));
( Y6 L* v. [- J& ^) t Y6 o5 B }
; c* N8 t1 u3 J( `: ~6 D这里面if(moneySpace.getObjectAt(x,y)!= null){
) }2 m {" R) g8 t$ h7 F I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();* s4 V3 K, b+ `
}
, q: X f; g+ {7 T; _ G' W5 v else{
# Y4 q+ Y0 [# I. f" g* g1 K I = 0;
0 R4 a7 f# _4 R) o是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?$ U" r! S5 L/ o! \0 ]1 [7 n
初学者,还请见谅! |
|