|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中0 f& T# z! ~% e. T
public void spreadMoney(int money){! @9 S; D, `+ _+ [. d4 w4 \; b
// Randomly place money in moneySpace0 ?& }& u `7 Z; O( I
for(int i = 0; i < money; i++){
7 j( ^ j+ D# K5 p
$ |0 ^- F5 h4 G$ M3 ` // Choose coordinates
% h7 `5 [6 _' m% L( j int x = (int)(Math.random()*(moneySpace.getSizeX()));
& _: u% @8 ]( Z/ |4 [ int y = (int)(Math.random()*(moneySpace.getSizeY()));6 }/ _* g9 X! ]0 k
2 j9 c* m5 N; N/ d8 s
// Get the value of the object at those coordinates
6 @8 U$ c" z: s int I;
& x) r; ]* J& J/ M' L6 I/ g/ k if(moneySpace.getObjectAt(x,y)!= null){: ^$ l( ~; k8 o5 U4 C
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
7 M0 v) S4 F# S: n }( ~& i U8 D; z
else{
& U7 h; A+ w# o6 w" |% K) H: X I = 0;
# S: U6 J( u: w* r }% \* G; C- o+ J, H' ?5 `1 j% d
// Replace the Integer object with another one with the new value( P5 s! E2 a ?1 w( l
moneySpace.putObjectAt(x,y,new Integer(I + 1));
2 B) S2 e5 Y1 f& {- | }# Q7 i' ~* k) c# ~
这里面if(moneySpace.getObjectAt(x,y)!= null){4 m( q. F" Z( C! a+ H3 G) T8 r
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();) I( t! W" v) O) P1 ]9 _' q! y
}/ N4 V) P( z9 K) N$ i& { Z+ |
else{
2 B8 b4 W9 K3 H: u6 \0 ] I = 0;
* Q& I) l+ B. k8 v' B U. g是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?( N/ Y9 E6 {; h
初学者,还请见谅! |
|