|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中* l4 u5 y, P- ? @ U4 |
public void spreadMoney(int money){3 V9 M' Q/ H! H. a' u! h
// Randomly place money in moneySpace$ W6 f2 v" P I5 z# p. q% a1 t
for(int i = 0; i < money; i++){
4 N, O0 C) Z" Z5 U6 \' A- ?" Y
8 F* p2 ~7 M+ I6 I0 } // Choose coordinates
7 z2 N9 z8 u3 i: g1 O int x = (int)(Math.random()*(moneySpace.getSizeX()));
" L) ~8 P. J1 g& s, {) x. { int y = (int)(Math.random()*(moneySpace.getSizeY()));2 y+ w% w+ ]2 [5 y) v
- g9 H8 A( p. i- E // Get the value of the object at those coordinates
# O, F$ x8 |$ F ^3 S- n int I;4 Q* H: Q1 N* ?
if(moneySpace.getObjectAt(x,y)!= null){
! j2 A, G5 a$ h; ]# Z8 p) i I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
% ^) {% c/ ?, X5 ]+ A( R }, g. _5 ]: P5 a( N( g% ~! @
else{
i" G1 J* B3 D, ~ m/ F3 ^, H, p I = 0;
9 C. _; o! H+ X' y3 E! t }
. P6 |; @# p2 U7 j5 o$ | // Replace the Integer object with another one with the new value, j' P8 E: E8 w" c! |: O
moneySpace.putObjectAt(x,y,new Integer(I + 1));
2 [/ P& ~+ b9 ^* D3 l ~. E. h9 q4 i }
+ E. T& \% ?8 a) a, e, J这里面if(moneySpace.getObjectAt(x,y)!= null){
: ?4 l: C; z6 x1 _. A z1 S6 ? I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
, n3 V1 q$ J3 s8 f, P }
( r6 t1 U1 W/ J$ h( g. t) s8 [ else{
1 R, c( ~6 Z% T! k" w" |2 A" D I = 0;8 x5 W( F, f" [" K
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?4 y. i2 U3 c9 v0 P" k* K0 O. Y
初学者,还请见谅! |
|