|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中% g! O; c0 T4 o+ U- z3 a5 V8 `; n
public void spreadMoney(int money){6 g; B- H3 K# Z$ J
// Randomly place money in moneySpace
% V3 o( Z7 I6 t% F for(int i = 0; i < money; i++){
* C1 N- E" B3 ?2 [% y, b: v% I/ R7 m, T/ p' l/ O" S7 t
// Choose coordinates
! \3 f" B0 }% i& K- L, O int x = (int)(Math.random()*(moneySpace.getSizeX()));3 _& D# [9 \! k1 m; w* w
int y = (int)(Math.random()*(moneySpace.getSizeY()));5 x' u$ e6 Q/ v% O
4 _( G" n8 Q+ |: B Y& r4 K9 K
// Get the value of the object at those coordinates
3 I9 y3 X/ [+ ^# R& `6 Q int I;; I8 a# o% r! ?& R
if(moneySpace.getObjectAt(x,y)!= null){! a( l+ l# p w+ D1 d7 r
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();+ o2 K+ T+ \5 v# n* i
}5 T# L7 i; x( H. }
else{+ n4 t: F7 r8 R
I = 0;
" c: @, T5 ~/ ~2 K# r6 s; R0 H. K }5 h$ ?. f" h( z/ U+ n) Q
// Replace the Integer object with another one with the new value
2 C7 A. O; w L1 @8 ?0 _2 k moneySpace.putObjectAt(x,y,new Integer(I + 1));
' R+ ?& w. ~+ B2 p9 c& ? }
; O4 z1 F' P9 t* H1 U6 b这里面if(moneySpace.getObjectAt(x,y)!= null){! ~* f' c9 J5 P! H
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
" F1 u p! g Y# l+ x# a C$ o }9 t" |6 }; Q! L8 ~5 W
else{* z0 M& D E; P3 ^5 ^, g# A% k3 {
I = 0;9 k' W* N9 B" E# R8 Q
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?5 J5 ~! f5 E5 a0 w- a8 z) l
初学者,还请见谅! |
|