|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
$ P% P3 U9 p& G: Rpublic void spreadMoney(int money){
% O1 l; C C; I( U; b& f // Randomly place money in moneySpace
6 v0 g. }/ G; A for(int i = 0; i < money; i++){
1 b+ U/ `" a4 }% Y
% X# u1 l- M" O$ ]7 A' s // Choose coordinates
) h7 c3 Y( k. P- V% {$ U int x = (int)(Math.random()*(moneySpace.getSizeX()));
; o; [; q: {- {' k5 h4 M int y = (int)(Math.random()*(moneySpace.getSizeY()));
8 _: D0 D! k2 s( t0 U$ R: Z1 x+ S1 r/ b+ a/ \- k
// Get the value of the object at those coordinates
- L$ J/ D/ h9 U& g0 @7 o. J int I;
# E0 Q4 Y) y3 O- h4 Z8 Z if(moneySpace.getObjectAt(x,y)!= null){
# R" u! n3 y2 m: Q6 n- X% a6 a, r I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
+ X6 B7 t! f! ]% B. M6 f* c }; G7 E, l0 [( v, I. [
else{6 o4 F: m$ e6 x: p* f! \+ H2 e/ \8 S
I = 0;
& M$ s9 a \4 T2 K1 |6 P G( c }
E$ f3 F: J! C. A; J8 S // Replace the Integer object with another one with the new value7 M+ h' @$ w: N! V+ T& e9 V. `! x
moneySpace.putObjectAt(x,y,new Integer(I + 1)); M% V8 i( \3 W5 S( g2 L
}( Y W' J. I- S- S% \
这里面if(moneySpace.getObjectAt(x,y)!= null){; E% Z6 U( @5 m8 r* k3 u+ J
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();2 W: F& M$ I! [3 `
}8 |* s3 Z" l& [, n6 [
else{- Z' T% w6 E0 k8 ~
I = 0;
$ v3 D }( M6 _. v- `3 g是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?, v3 e+ Q" ^! ]! V* d
初学者,还请见谅! |
|