|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
; k( B4 }2 l0 M; E( ?; M9 g( Bpublic void spreadMoney(int money){
' e8 [& ] I1 I, S8 k( n, z5 N // Randomly place money in moneySpace
. V' T* x4 }+ A8 ^ for(int i = 0; i < money; i++){
2 O, P( P& F% ~& e, v
5 G- F5 m: |7 G6 F1 l' b- A9 r y' Z6 n% I // Choose coordinates+ j+ g9 z& `5 {) Q. y
int x = (int)(Math.random()*(moneySpace.getSizeX()));. m* Y- V2 U4 M8 T
int y = (int)(Math.random()*(moneySpace.getSizeY()));4 T; h4 L1 T/ i2 B
* K! t& ?! ?1 c: u" l
// Get the value of the object at those coordinates
1 U) Z; o/ H% @2 w, \/ t- ~ int I;& f- z7 n2 B& l
if(moneySpace.getObjectAt(x,y)!= null){; w9 R& X4 i2 K3 s+ T) S5 B2 {2 D
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();6 Z J1 o* v% _7 [# X
}
+ \5 Q( o7 s* _0 D, \5 ? else{ ^& U# Z4 ]; g8 O; A, s8 R% A! N
I = 0;
! R* T n C4 L }
2 X- R- X5 m, }( N // Replace the Integer object with another one with the new value
; u- Q. S$ y& ]/ f% ` moneySpace.putObjectAt(x,y,new Integer(I + 1));
9 K. [9 ~7 i. V8 a }7 I) y0 M$ z7 d% d0 X
这里面if(moneySpace.getObjectAt(x,y)!= null){) W' P8 t! ?% H7 V* D
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
% j9 G- C4 Z. N* M& A' l }' f) ?7 r0 z, T+ R$ F
else{
& v- [. @) i. W% g I = 0;
* R# U3 {" Y1 S: c1 ]& y4 U+ d3 ~ S& N是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
3 ?7 X7 m/ `1 `0 ?初学者,还请见谅! |
|