|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中3 V1 G/ M* l7 }8 O
public void spreadMoney(int money){9 F/ e. a4 R1 @' ]
// Randomly place money in moneySpace: k, C: ] o3 |! G5 p
for(int i = 0; i < money; i++){" w5 Z ]9 I( \; Z h5 k' S
; s+ a( l* L: \ // Choose coordinates
$ L; H& b5 l3 l8 F7 Y int x = (int)(Math.random()*(moneySpace.getSizeX()));) N, `" J7 q5 ? |3 X% n
int y = (int)(Math.random()*(moneySpace.getSizeY()));
6 K9 c9 K2 g3 }: {& _9 u- g. N' n1 {6 C" ^! i% T0 D6 C$ O8 B
// Get the value of the object at those coordinates
. R0 }9 C, c* r6 H1 p int I;
( ^" T' n. N6 I0 U9 K! ?" g1 w; N if(moneySpace.getObjectAt(x,y)!= null){
' U0 x) Z2 C6 H+ N I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
r2 ?8 f0 T' k8 z3 T2 C }
$ M" r0 v2 O! c4 @# U else{, R( y% X: |5 x3 d/ K; R
I = 0;
, x3 F. v5 R2 s2 h }" n* I) V. `5 @* m$ p- U* \
// Replace the Integer object with another one with the new value! t; V1 O8 r9 p3 x7 L* O. P* `
moneySpace.putObjectAt(x,y,new Integer(I + 1));1 g+ s [7 z: O, \/ {$ i9 ]
}
: u: r$ R- `, ]. h! n X6 U) M这里面if(moneySpace.getObjectAt(x,y)!= null){
+ [2 s- O. e1 [ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
$ k |- f9 R W }
6 v$ L. ~4 V/ c5 ?* E* e, L else{
8 m" C- @* l5 }: w/ N I = 0;
c' j2 E' [! } v% f% G是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?0 \3 U8 }! q. P4 p/ H
初学者,还请见谅! |
|