|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中& ]2 G$ w+ X: N* f5 c, |
public void spreadMoney(int money){4 t$ o( s0 y/ |" U4 h8 B4 w I$ M
// Randomly place money in moneySpace, i& w( F& s& v
for(int i = 0; i < money; i++){
" e" N5 Y! v9 ]
% i7 C. z, g* k- a# Z- u. u# J // Choose coordinates5 z. [' e @" ~) y! h2 ]
int x = (int)(Math.random()*(moneySpace.getSizeX()));
/ v$ U4 Y$ Z" v, @9 ~8 O4 Q int y = (int)(Math.random()*(moneySpace.getSizeY()));% F& K- ]' t7 [3 s
) o3 r& L/ r0 [7 I7 ?/ a // Get the value of the object at those coordinates
/ g& W/ P8 D* L6 s# [2 y5 U' j int I;
1 N+ Y, G* Z2 {: Q4 n if(moneySpace.getObjectAt(x,y)!= null){
8 A+ W& B9 w5 C% \ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();# @2 C. C+ c& O/ Z5 N! E5 `0 w1 T
}
) b. A! Z7 D2 T' P* W else{
1 q9 m2 P. I& x( O/ C) b; w9 A- ] I = 0;
" T6 V6 N$ S* [& ?0 }9 x }
' p- c9 M% a/ z) x1 f( K // Replace the Integer object with another one with the new value
: w6 ]$ s$ N$ h( G) c8 D" w moneySpace.putObjectAt(x,y,new Integer(I + 1));7 v# e j/ M5 Q! `
}0 C7 ^: m) `8 l3 d. S& R3 h3 P
这里面if(moneySpace.getObjectAt(x,y)!= null){
0 D9 w2 y% ^9 }3 m I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
. w' v O% O \. L' Y% O" G }
' o/ }! S" m/ u else{( S# ^" G7 I( W3 u1 y6 M' j% a& n
I = 0;
, t/ A' r! }6 y" }是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
0 P- O' @2 D' i* P初学者,还请见谅! |
|