|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
& `8 j, y1 R! @6 z* F* Ypublic void spreadMoney(int money){; @# \! g$ f: V) o5 B/ u2 f! I
// Randomly place money in moneySpace
2 I% I8 N& S* \ m7 P" D$ v' W for(int i = 0; i < money; i++){- R" Z3 J% {/ C+ W i: O& j
% F* }7 I' Q7 E$ Z // Choose coordinates
$ k) g0 b5 E6 ~8 f9 P+ h7 X int x = (int)(Math.random()*(moneySpace.getSizeX()));
, q$ @! I8 F% R8 T. D int y = (int)(Math.random()*(moneySpace.getSizeY()));
2 V2 ^) N1 ^! @3 D. G# e) F! f# q! Z$ f9 R
// Get the value of the object at those coordinates
" I1 f5 l9 M8 { int I;. c! b/ J$ b7 d% P5 ]
if(moneySpace.getObjectAt(x,y)!= null){
1 `, }3 ^' O d6 E: ^8 _) v I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
# U# V' E7 e7 ?0 C9 X }9 C+ l5 Y" N" D- o1 ^0 W* U4 m
else{
: |: t' q+ f: \; z! o( P I = 0;) V0 d" v7 n v0 T8 h1 x A
}0 \' R! w: }9 {) `
// Replace the Integer object with another one with the new value
9 o% M+ D* a; [! k moneySpace.putObjectAt(x,y,new Integer(I + 1));4 c- L6 E# H6 S) f) D) W
}' H! C f/ Y8 w" P
这里面if(moneySpace.getObjectAt(x,y)!= null){9 ^8 S& ^& Q& ]2 M3 m. _
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
5 J) t9 x( J. I( R! C I9 }0 { }
2 P1 j& [' G ^0 D m& E# ^ else{
4 R+ \) ]& d0 n. n* E( x I = 0;8 e" {1 _% _8 D! _! t3 x
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
0 J2 N5 R8 }- j) g0 M; X初学者,还请见谅! |
|