|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中7 b9 o3 h# X* G% {( n
public void spreadMoney(int money){
; G$ j) I' F$ T: k // Randomly place money in moneySpace
8 i! `5 V3 x: {3 _( t5 Q6 m for(int i = 0; i < money; i++){( e B$ y$ k/ ?
% K$ Z4 d/ z4 y6 K% Z // Choose coordinates
7 p! h- y- P( X% x3 r! F int x = (int)(Math.random()*(moneySpace.getSizeX()));/ m* L3 k2 z. L% T
int y = (int)(Math.random()*(moneySpace.getSizeY()));1 c5 |5 t ~) |
; v2 K+ ~ s' _
// Get the value of the object at those coordinates
! B% M9 {3 w$ s; D) s) r, V# K) x6 Y int I;* h+ K- g* J* v& j* e( k- v: R
if(moneySpace.getObjectAt(x,y)!= null){
8 g9 g* P2 j1 G7 s, [5 b I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();5 {' H6 Q' v+ l
}
2 r9 |8 @' M0 Z1 D1 \. p/ B6 g3 j! | else{
: k6 A6 W5 _$ j: }4 D, g+ H I = 0;: B g7 k" c- `
}, x6 e/ W0 M ] T, J
// Replace the Integer object with another one with the new value
$ E& g1 ^* \* d( s' p- J# u! E moneySpace.putObjectAt(x,y,new Integer(I + 1));2 D" D/ Q1 y. f3 k
}
" c" ^' ]0 R T7 G这里面if(moneySpace.getObjectAt(x,y)!= null){
. D+ r4 E3 t3 T0 V* A! h I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
$ W$ d8 F: G: ?3 j }2 O0 Q/ ^* ]+ a2 T$ o$ s
else{
7 L' @, g7 g! D( m: r6 f& C I = 0;: B% U/ \3 [0 V3 f
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
, ]- x; O! O8 ~" h$ Q, {3 U初学者,还请见谅! |
|