|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
" o E& d4 h* ?7 Q/ Npublic void spreadMoney(int money){0 i1 {) g2 ?; s. u5 p' P* k a$ i% s
// Randomly place money in moneySpace
% Z. u7 O4 J4 v3 Q' O, g for(int i = 0; i < money; i++){
/ W: e$ B6 v7 V4 {6 _' x2 _+ ]/ l2 K. T+ N( z9 n2 f9 {: F
// Choose coordinates
! s8 p; k$ j9 B$ D* z) n4 ]* z/ a int x = (int)(Math.random()*(moneySpace.getSizeX()));
- m, f7 h, K# i) y/ _ int y = (int)(Math.random()*(moneySpace.getSizeY()));- M' b. a% W# x! E/ i0 u" b) ^
! M2 c, K* T8 O* G q- j
// Get the value of the object at those coordinates
q( H2 K: f$ V int I;; N6 O! Y/ _; U1 o9 D) R9 N
if(moneySpace.getObjectAt(x,y)!= null){- m. G& l8 y7 H; G p0 D& ]& |$ K
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();9 L8 X% k( r, k5 H* @
}
! ?0 u' {8 A+ L7 w8 ] else{
' |6 R5 n; I% O- L @3 q5 i I = 0;7 G: }- N7 s/ l/ ?3 Z# ]
}+ S% O: f* I/ h" A
// Replace the Integer object with another one with the new value! y2 C& y9 Z& e1 W
moneySpace.putObjectAt(x,y,new Integer(I + 1));5 {9 G% S* a% B$ c
}1 u* h8 H$ w, y5 k$ e, n3 R
这里面if(moneySpace.getObjectAt(x,y)!= null){
8 v' D/ b7 X% F I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();; E) a$ o! [) t% v+ s/ ]
}
' T; z+ | |* W& S- k# R8 O; {7 A8 j else{
% u4 H- e& j/ u I = 0;
- B. Q) z5 q6 Q: z$ x是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
6 q" w9 ]7 |$ [% X, j9 F8 g初学者,还请见谅! |
|