|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
" v! _. ]$ C. o( m1 spublic void spreadMoney(int money){; B+ t5 j8 v' s/ @$ o" C
// Randomly place money in moneySpace3 y- P9 w. x- z ?8 ?
for(int i = 0; i < money; i++){0 \% i% i; w! K& z
_* Y7 ? E, o0 T) \
// Choose coordinates* O9 N1 t* S' \3 ^
int x = (int)(Math.random()*(moneySpace.getSizeX()));
+ S0 W" e& Y7 o0 y# C+ Z v int y = (int)(Math.random()*(moneySpace.getSizeY()));
3 s, h" Z3 P, F9 I' T' H( I% S) v3 J4 |# g) k
// Get the value of the object at those coordinates' j$ z: K, `$ r: {3 Z G
int I;2 N3 S& `' u, _) I& U
if(moneySpace.getObjectAt(x,y)!= null){
8 a }1 q: K1 G I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
" s: E) o1 b8 F8 n! T. Q) a, v }
3 @2 I5 M6 X! |. M1 x# {1 ` else{4 ~: y: Y1 M" F* _
I = 0;
# c( u. T/ T7 z }
6 w; l' j( H$ e6 ` // Replace the Integer object with another one with the new value
* z+ s5 c2 }* s: X; h* v% f: {# E moneySpace.putObjectAt(x,y,new Integer(I + 1));
$ M% t U3 g) {' E, Y" G) F }/ w$ b8 G% f$ Y7 g2 v6 r9 Z( r
这里面if(moneySpace.getObjectAt(x,y)!= null){4 v6 A' \. I! P% W" A9 Z/ E) K
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();5 N9 g& R0 T" x
}
/ {) [) [, n! S, ~% l3 r0 } else{( H0 @" l* V/ T4 K5 D8 `" ^6 Z
I = 0;
& [/ G; \- Q; l是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
( ~3 h% G* W1 T- Q) \3 j7 A初学者,还请见谅! |
|