|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中# @5 x( C/ K$ S/ G4 s* h) q
public void spreadMoney(int money){6 F4 g8 ]4 Z: b. r$ \! w
// Randomly place money in moneySpace9 Y7 g/ V3 K- A+ j/ ^. f' \
for(int i = 0; i < money; i++){
7 H* I8 y! U. J0 n- \5 A2 _
3 d4 V* Q% e6 Q( B // Choose coordinates
) L3 k2 A* F: J2 E; J. D' n0 ?0 E int x = (int)(Math.random()*(moneySpace.getSizeX()));) I/ U. W+ {' _" f. g; c3 w
int y = (int)(Math.random()*(moneySpace.getSizeY()));+ s. H$ p) K% f
% n0 ]& Z* Z+ b, P3 m // Get the value of the object at those coordinates
4 y. L) E( c- Z9 }; ~* w& Q int I;* {4 r$ n7 P1 v' u, z6 [ \
if(moneySpace.getObjectAt(x,y)!= null){2 `3 x# A3 A4 Q4 b& U
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
- f* m( a$ [" Y5 D% Z }: q! x8 E( l7 g2 Q2 N% a
else{3 R9 f D! G; |1 Z
I = 0;
4 T( N2 g/ H6 x/ g1 p5 [0 s0 I2 h) s6 ^ }
3 y% u# \2 s% z! @. D4 F // Replace the Integer object with another one with the new value
8 a2 h9 p/ c5 P' Q moneySpace.putObjectAt(x,y,new Integer(I + 1));
; M0 ^" z* V. M2 ^8 F1 ] }
3 N2 V* J2 U P0 z9 e这里面if(moneySpace.getObjectAt(x,y)!= null){
; W0 q$ T, c, v2 n+ B2 o2 G I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();4 x! h2 y% I3 n3 V' h
}6 i8 ^. u8 A: {
else{# W E& L5 `& k3 m! |" M5 }9 n
I = 0;; m, ~" D5 U% ^% s4 g3 {
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?# X" L5 _" O$ {1 i- I, P0 ^
初学者,还请见谅! |
|