|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
% k7 w4 i: m2 w9 U1 G. g, S1 _5 Fpublic void spreadMoney(int money){
+ w* m7 h# k* v$ d // Randomly place money in moneySpace
" s& w3 j7 u' F9 ` j$ g0 K+ \ for(int i = 0; i < money; i++){, J+ m! {2 s1 D
/ G8 e( U4 `1 e9 U; i I, z! Y // Choose coordinates
: S; _+ R6 x& q" P int x = (int)(Math.random()*(moneySpace.getSizeX()));
- E, }$ }/ \* v$ [/ h int y = (int)(Math.random()*(moneySpace.getSizeY()));
( B# X, l. q9 w! |8 }# z6 p4 D2 D2 h& M3 C6 d7 k* ^
// Get the value of the object at those coordinates
, n: I# [ \: ]& X* q int I;
/ f1 N9 l5 V5 { if(moneySpace.getObjectAt(x,y)!= null){1 _ {& b; i6 }* S& K' W* L6 V
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();9 a G. f5 X: n6 P1 M9 r, O# ^& o
}
" r8 b% I8 g( \% y" ?. s: x2 @ else{
# \3 Q+ a2 t- O1 q I = 0;
) R* U& C4 ~: M }
" z# F3 V) s6 V0 Z // Replace the Integer object with another one with the new value
1 H5 }$ }& j+ e; @ moneySpace.putObjectAt(x,y,new Integer(I + 1));: Q$ ^9 W% I$ B* Z
}
, d) H! i/ p {5 N, V, V: v这里面if(moneySpace.getObjectAt(x,y)!= null){. W3 o/ s; J; @: A$ S7 Y
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
9 V6 p3 e z! }5 ^9 k" J }% c! h9 P6 Y) n# M: Z8 {) K- P
else{* w1 q' N/ D# J& y) R+ `
I = 0;
- h+ a8 g% `. v; A是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?- D, M- E" ?4 P* g8 h
初学者,还请见谅! |
|