|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
+ e& h! K: E4 \6 ]$ y& bpublic void spreadMoney(int money){& H7 i0 N" w4 ]
// Randomly place money in moneySpace
" U% \ Q% O6 J6 k+ ~% S for(int i = 0; i < money; i++){ z* s/ K% S9 n$ L
* h8 @7 S2 Q/ e4 V' G0 t! j
// Choose coordinates
/ l# c& z4 }/ a- E3 g int x = (int)(Math.random()*(moneySpace.getSizeX()));/ D. S) W. i$ z2 C' A3 u
int y = (int)(Math.random()*(moneySpace.getSizeY()));
: H8 d0 k/ f2 b3 H! s% h5 G u- T# u7 _
// Get the value of the object at those coordinates
, Z; R3 Z$ B. V% A/ ?* c int I;5 R2 [3 H9 [6 y- r* T+ K$ I
if(moneySpace.getObjectAt(x,y)!= null){
) e* M3 T9 d# p I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
; v+ s# \, k% T) T" X7 W- O }
- T0 }8 @* T" n/ | else{1 r4 D& w3 N) m6 `
I = 0;
# k3 v3 @7 `6 i9 \ }8 G- y1 F2 n& R$ m
// Replace the Integer object with another one with the new value
" l! R& d# B3 _/ Y, q N p/ u moneySpace.putObjectAt(x,y,new Integer(I + 1));
4 {3 t4 ]7 L2 K4 g9 V8 ? }- H. _6 [! B }: b
这里面if(moneySpace.getObjectAt(x,y)!= null){' ~9 S7 B3 K; H3 K: e
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
- g9 g$ r& g' T# p0 d; z }
* V. r' H% Z8 I" } else{- l+ j {0 H/ T1 N' e
I = 0;" E) H) `% U) ]# f
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?: R* f; ]5 a$ t( M" \6 y+ m
初学者,还请见谅! |
|