|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
% O. e7 \/ A" @# Mpublic void spreadMoney(int money){
/ |+ T; P. H. [2 ]# c+ x9 D" C // Randomly place money in moneySpace0 {- U, ^' i( H9 m
for(int i = 0; i < money; i++){
0 W% k1 ?! ?) y- P1 q+ p( d! L; f5 i# T4 q# p
// Choose coordinates
! k! l5 M$ p# l/ `* m1 Y& ~& N) b( ^ int x = (int)(Math.random()*(moneySpace.getSizeX()));
& p1 S: k, a7 L) P# _ int y = (int)(Math.random()*(moneySpace.getSizeY()));% l+ ~6 u" Q, D6 t7 v
; Y# \$ c+ D4 a/ m
// Get the value of the object at those coordinates/ }" P, K* g' x0 w" B4 P
int I;/ M& q! r$ G' t# v2 U8 t' W5 T' Z7 x7 |
if(moneySpace.getObjectAt(x,y)!= null){
' n, ^4 e' |8 y I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();, L3 B+ [+ V/ b* R
}
% }) l z* N8 m8 B3 m5 U, r. p8 a: @ else{3 D# Q7 c3 _, B0 k3 D' p
I = 0;, Q) q. c. q) {9 Y/ `3 t+ L3 }) J
}% ^- `, v. s0 K ~+ }
// Replace the Integer object with another one with the new value
2 M8 D: t" O3 {7 X9 s6 Y moneySpace.putObjectAt(x,y,new Integer(I + 1));
6 m$ Q5 K9 B3 J- b9 } J1 @ }# U3 n" H+ `4 S; Z% z+ K
这里面if(moneySpace.getObjectAt(x,y)!= null){5 {. {' a( k7 y& n1 N" J2 o @$ ?0 A
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
1 ]/ m% z& [" l ^' |. |% b }! R6 P! ^% U# R( @# Y7 V
else{
$ l; [3 G# _" b I = 0;3 U8 R9 s7 O" L" I% C, r
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
! u9 d- {. h1 \5 w! M! W$ L8 b5 H初学者,还请见谅! |
|