|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
, m H' z1 B \$ O1 t1 \% {public void spreadMoney(int money){9 A! ` y, I. m3 B4 D# g
// Randomly place money in moneySpace
2 s u- _) W" j! _# K; X( | for(int i = 0; i < money; i++){
9 [6 r/ c. r8 b& \9 B
" S7 K# ~1 j3 a+ z K$ Y // Choose coordinates5 `% d% l3 D* z( `( c
int x = (int)(Math.random()*(moneySpace.getSizeX()));% B' g' d; L9 i2 r
int y = (int)(Math.random()*(moneySpace.getSizeY()));
/ M; o7 Y9 u9 T& Z# c2 r8 |- f% l/ M% _5 i+ |7 T: o2 t
// Get the value of the object at those coordinates
/ N/ {& S" ?" l0 W int I;* R! x7 j0 e0 M$ o: H r/ r# P+ b& v
if(moneySpace.getObjectAt(x,y)!= null){
' v, a- U' U2 U8 F+ C L/ D I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();- C N/ q! M3 b" [8 q3 `, u
}
6 `! R- C9 d; k+ U2 K else{
% I. p: W) H8 `$ P& h I = 0;
1 d3 Z0 Y u3 U }1 L: z8 t, k/ o) ?
// Replace the Integer object with another one with the new value' u; E8 L5 l5 }- D- V y
moneySpace.putObjectAt(x,y,new Integer(I + 1));. q' _4 \, `& {# J! `$ k- z( k& O8 f( t
}5 r8 U" z; U% m
这里面if(moneySpace.getObjectAt(x,y)!= null){* g* n6 i( l: `5 p5 |* h/ q
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();/ [0 |& B* P F% L* _3 h8 F( s* V3 t
}
/ f; v$ l' H8 ^, p else{7 T5 U) ^ X; u; }
I = 0;
" f7 P$ m. Z% O' T4 n* }是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
1 ?8 D0 v6 ~7 b# _+ n初学者,还请见谅! |
|