|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
! z# t1 I' Y2 U' }public void spreadMoney(int money){
; ~9 R1 ?' Z6 [! @, [" x2 y5 c( V // Randomly place money in moneySpace
" V" Z+ L# u7 `( ` for(int i = 0; i < money; i++){
! R5 d L: `0 R" u
, [7 Y6 e% S- B1 ?% x // Choose coordinates7 v! o- D% R! `: t8 m
int x = (int)(Math.random()*(moneySpace.getSizeX()));
$ c) e8 t3 ]! U5 k: W int y = (int)(Math.random()*(moneySpace.getSizeY()));
: Y# l7 l' K' @
; m/ B( a8 B4 K6 N: f: Y+ I // Get the value of the object at those coordinates! Y% K" g% H+ S6 M
int I;& G7 B* k# I' J3 F' S/ \; k
if(moneySpace.getObjectAt(x,y)!= null){
' `" }8 F; I# `' p I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
- X1 o7 e( P0 w }! ?! O" O8 l7 a3 [/ R* }
else{
+ y/ B r) s5 ]+ z. T I = 0;0 U+ T3 [( z9 i7 ~' z1 r
}5 m4 M) B8 o- e5 |6 _9 Q- _6 ]
// Replace the Integer object with another one with the new value
* x& q2 ]3 O. @1 s! } moneySpace.putObjectAt(x,y,new Integer(I + 1));, X- O/ A8 a7 D; }
}
( x% ^+ c+ `' x这里面if(moneySpace.getObjectAt(x,y)!= null){ |. V& z: S) c
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
3 g$ u3 B; M, S8 n }
/ X$ ~+ p; g7 O0 j& z. {5 H else{( w' L7 s8 [6 c. i- d
I = 0;
) a# t) ?9 c9 u% N$ b! t是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
1 z8 l; g, T# o1 p5 C& C; B( [初学者,还请见谅! |
|