|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
( n4 b2 _: {7 Y& `. [9 }public void spreadMoney(int money){# N( f, J( i0 D. B
// Randomly place money in moneySpace
# M2 v. l# J/ w% U2 r d7 g0 y" _ for(int i = 0; i < money; i++){' {6 w3 V v2 ~* V4 s
3 x6 |' C7 x3 i- Q7 ]$ d( g // Choose coordinates
* U% j% k) Q& \5 P: ] int x = (int)(Math.random()*(moneySpace.getSizeX()));
v6 X6 D0 m* Z! b int y = (int)(Math.random()*(moneySpace.getSizeY()));
" Z8 J' f$ d- n
: W. J1 d# Q X* l' v0 U1 y3 @! i // Get the value of the object at those coordinates
l1 s( T# S0 A9 L, @* Q int I;+ \9 z _+ U* p$ c W0 d T
if(moneySpace.getObjectAt(x,y)!= null){
* A `: k$ U) h I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();2 M# S1 g4 {8 j6 E/ A' ~
}
$ o9 S. [1 Q: G; s% C6 E% D. N) _ else{
# n7 A& F! h; |+ }9 k I = 0;. N7 i8 o3 O* }" \0 I7 ~
}1 c% i; \" }/ Q0 q) d+ q3 R7 j9 {, i# Q
// Replace the Integer object with another one with the new value
! q3 F5 }# N7 _3 K* y moneySpace.putObjectAt(x,y,new Integer(I + 1));' h$ q& w/ E3 e! m; O7 q* v
}2 P6 M( K9 Q8 r
这里面if(moneySpace.getObjectAt(x,y)!= null){+ z4 B# r( `2 |' V& w( P
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();$ V/ f8 ?0 P# N9 z, F# C
}
, J3 [3 U9 ], v; z8 v else{1 v& J0 t% W0 K% ]6 X9 _: j
I = 0;0 M: D% R# y6 L/ ]5 Y5 h
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
% J3 J# X* M# H5 \/ |- S9 N6 w: d初学者,还请见谅! |
|