|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
! ^' _7 ]2 L! z+ N/ Spublic void spreadMoney(int money){) I+ ^% ^4 M y' Q2 B+ V4 y3 o
// Randomly place money in moneySpace8 x, j- ?0 O9 m, T
for(int i = 0; i < money; i++){+ G x u l$ u5 u( P
1 |' o' }5 j) Q# g9 @( ? // Choose coordinates3 g4 ?0 k) H% _# \( u7 P( H. [
int x = (int)(Math.random()*(moneySpace.getSizeX()));
% ~: A: R% @2 y$ [: u; _7 l int y = (int)(Math.random()*(moneySpace.getSizeY()));# t/ R6 ^- x; p6 l& h
; Y1 w3 s3 I5 C9 o1 z" q: J // Get the value of the object at those coordinates1 F* [7 \; J: Z- n
int I;8 w, x& t& S" C% ~( [; r
if(moneySpace.getObjectAt(x,y)!= null){! u0 w* N7 _5 m; c+ d' d) w& i
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();7 a' X, G* s, c& X. ?% o9 x8 p% W# {+ F
}. n; s5 x+ T1 x2 O
else{2 S& T4 O- _3 o* T
I = 0;9 w7 V# z- e% H6 S: {
}
8 {: S& t [7 E0 f // Replace the Integer object with another one with the new value3 Y3 ^8 I: ]- K
moneySpace.putObjectAt(x,y,new Integer(I + 1)); Z& \/ x( ]) U! I2 [3 I1 @
}! I# z8 x3 ]& p7 O F" H* A
这里面if(moneySpace.getObjectAt(x,y)!= null){
5 t$ [' `2 K* K I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();* h4 j/ d5 g7 u
}
' ]6 I0 A& @. p I+ N else{3 ]# A1 D7 o; I K
I = 0;2 a6 l' @3 F( O# X
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?" X, D4 R7 N0 A- C
初学者,还请见谅! |
|