|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中: E/ H( q9 z$ W# O3 L/ I! J
public void spreadMoney(int money){* S" y# ?" X6 }2 j
// Randomly place money in moneySpace
9 q8 K* j% j) j9 n for(int i = 0; i < money; i++){3 v" F. u |* ~$ }7 w+ G
. O y! b% U0 W g4 b1 d: v
// Choose coordinates
: [, e2 z+ o1 R) G" Z* s- K% T int x = (int)(Math.random()*(moneySpace.getSizeX()));
/ J+ g9 }7 b3 \* c int y = (int)(Math.random()*(moneySpace.getSizeY()));1 @- L1 Q& M& R( a9 a0 Z; o; l
: s3 ]7 N5 Z: W* m6 L: s
// Get the value of the object at those coordinates
8 H1 u5 Y9 o! J1 Z2 b int I;$ S. `! v8 b9 }4 M$ I8 w
if(moneySpace.getObjectAt(x,y)!= null){
6 S9 {1 R$ a4 t; ~) O I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
6 @/ X' ?, p$ v9 e7 g% m4 K J) V) r } j6 y# I9 C7 @4 X4 }: h+ H
else{
0 Y, S" R/ Q) c/ ~4 }3 t0 P I = 0;
9 I. f; w9 Q$ ?$ Z7 J: B/ H }: H0 B: K6 P5 t* @6 R
// Replace the Integer object with another one with the new value
?8 X3 v5 f- N W/ l6 } moneySpace.putObjectAt(x,y,new Integer(I + 1));
% y4 Z* l; j/ G' `* ^6 [9 U }
: L6 O- w$ W, a这里面if(moneySpace.getObjectAt(x,y)!= null){9 `" @' J6 Y" o7 F8 y2 Y3 c# R
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();- v& F- ?3 Z# F; T: B
}
) v/ O# v* G& U; ^0 ?; U" O6 p else{! H4 p: v8 R) L( f
I = 0;7 B- X! U0 T- Q5 L) e* }
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?% k: N4 c" f8 o- G
初学者,还请见谅! |
|