|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中3 T) `1 V) T& N* }
public void spreadMoney(int money){: o. P% @; f1 c
// Randomly place money in moneySpace
9 h9 E6 M: T3 I, O* o; F1 } for(int i = 0; i < money; i++){6 w6 ?& v4 E5 @2 A) `; p/ P
: u+ @% D% R8 ^9 n4 L0 U: s/ B X // Choose coordinates
, v. P7 |% ^' d& W u! [9 ` int x = (int)(Math.random()*(moneySpace.getSizeX()));
& D- t/ K2 v) F5 U/ l1 N: Z' n4 ~3 o int y = (int)(Math.random()*(moneySpace.getSizeY()));
& U# P3 u- n5 I, L
3 m _$ {# h# l1 g) |: S8 c6 g // Get the value of the object at those coordinates; A1 }3 R3 ^5 [$ j, m; K
int I;) s- F& ?2 ~4 m
if(moneySpace.getObjectAt(x,y)!= null){
0 K% j; G; l) ]/ f5 e4 S I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
3 I( N9 t3 @ w) P k& k }
1 }8 N5 U- U- W9 c# z0 u C else{2 }- G3 D0 q7 _' j( n, e& J8 w1 Z
I = 0;3 a! o5 f$ E, E
}" j# y" b# k0 A7 r* }+ G. b" {
// Replace the Integer object with another one with the new value
( K3 K6 \0 u& s; x, U moneySpace.putObjectAt(x,y,new Integer(I + 1));1 a5 O; g) b2 c/ l, M+ V4 n
}# I1 Q, W2 O: x) k0 g4 X$ f
这里面if(moneySpace.getObjectAt(x,y)!= null){ z& H$ ~* K' k0 j# m A- p: q
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
2 i z0 s5 ^/ g! k8 E( S! ? }* T4 R: u& D t0 O1 i( ?
else{( c7 Z) S4 h2 ~4 y( M; x
I = 0;
2 D0 g$ @6 V+ ]0 |- e# J是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?- p+ G+ g8 }1 d
初学者,还请见谅! |
|