|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
& F- u2 e0 p) d# \public void spreadMoney(int money){
% |) J" i) [( l+ R% X3 [ // Randomly place money in moneySpace6 l2 J3 G5 k+ h. X8 J1 e7 F
for(int i = 0; i < money; i++){* |% W. W& `3 n* e8 R! N
" o' \ X; ?' {) G- {0 S
// Choose coordinates/ k G; i7 {4 I, \. W6 u. V
int x = (int)(Math.random()*(moneySpace.getSizeX()));
9 |7 D4 [ l2 {# O9 ^5 \: {* f# t int y = (int)(Math.random()*(moneySpace.getSizeY()));8 l F, ^; ^* j4 P! P5 H/ s- X, r4 ]8 \
/ r% i1 t. b3 M' A2 h% P" l: ^ // Get the value of the object at those coordinates0 `' A' u: A: \" b) }0 X
int I;
6 O: [) D7 r# g4 p) F5 ^ if(moneySpace.getObjectAt(x,y)!= null){
* S w8 ?1 f/ K2 Z I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
! [) J; U) W z) _5 [/ M; K m }
7 }$ B/ }4 ^0 F1 Z9 e else{. T3 P5 I" B# l" Y# k3 K3 o
I = 0;
' {/ m* N; P) |& |& l! P }- W! D7 {% q# A" [$ m- J# |+ F
// Replace the Integer object with another one with the new value' {1 a0 Z0 F) Y+ y6 C
moneySpace.putObjectAt(x,y,new Integer(I + 1)); a# a/ s/ d( l& R* [5 |
}" _- D; A# {/ j8 J
这里面if(moneySpace.getObjectAt(x,y)!= null){
! |1 b/ |( H5 E _4 M& T I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
0 Y: X7 Q1 m4 S& C }
2 T' b+ s( d) n, e# l else{
9 j# K! I a" E! E8 }2 O I = 0;
7 g4 ]! U6 h: [* T! g是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?0 J" X% @- Q' F" q7 `# q- I' g3 x
初学者,还请见谅! |
|