|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
. J# F; i% n& C& mpublic void spreadMoney(int money){- U/ {2 m! w/ T, l* Q8 t
// Randomly place money in moneySpace
9 k% l! K& q0 d5 j for(int i = 0; i < money; i++){
7 B4 Y4 z8 t, D5 G6 M
! g6 t" m& {* F; w @, u7 k3 F // Choose coordinates- q1 h& T% N2 J' n2 f
int x = (int)(Math.random()*(moneySpace.getSizeX()));
X1 y0 P U. b6 [5 a int y = (int)(Math.random()*(moneySpace.getSizeY()));
6 Y: v- d% K" a$ N, l& v A+ z) W( |, u5 q. R
// Get the value of the object at those coordinates
7 F% ?. U8 m% W6 g' \ int I;
& s6 o4 b6 I. C$ I0 r- M* p1 d7 d- ?$ g if(moneySpace.getObjectAt(x,y)!= null){
- u5 M/ B4 u4 l0 X& C9 N# R0 d6 N I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();1 |* x. Q7 Z+ K* `* e* P
}
8 f9 ~) u2 l" z3 I3 R( |% _/ t else{$ O+ V/ M5 b( M2 L
I = 0;7 N7 u' A% Z- V" M. o7 v
}! O; k+ J' k2 B. K$ Y- K; N
// Replace the Integer object with another one with the new value9 I. `/ d; p, U g! N, ~; ?
moneySpace.putObjectAt(x,y,new Integer(I + 1));
! H _6 m; e! ~ }% X" O8 h$ N ~- B7 z3 T5 C% g0 C
这里面if(moneySpace.getObjectAt(x,y)!= null){
9 R& d1 Z$ Q9 m/ M+ o4 @: h# G! F$ k I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();; x7 s; I" l2 f3 h" {
}
/ P7 m3 q% b9 u3 N7 J4 D else{
( w2 B4 f( c8 \: I3 ~7 p I = 0;! F( t+ @* D! ^ u1 M- {. ]
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
5 y5 R0 {; H$ V3 I% T4 G) @初学者,还请见谅! |
|