|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
: G# o$ \: X O0 Q, z( y8 mpublic void spreadMoney(int money){. h" j; E$ G: T1 o `: Z- T
// Randomly place money in moneySpace
$ D% y! q% n- g; T( g for(int i = 0; i < money; i++){+ F3 j; s7 X6 d0 q4 }
( e" I( P3 g6 `5 i0 R7 w e // Choose coordinates
, c' r8 q9 R( Y# p! z( o int x = (int)(Math.random()*(moneySpace.getSizeX()));6 k# I9 v9 J H9 W
int y = (int)(Math.random()*(moneySpace.getSizeY()));4 B y5 ?+ W i& D
D6 l! m' w$ b( Z' f // Get the value of the object at those coordinates* P/ K7 F" A' v6 ?' u1 E
int I;6 Z4 c9 X; t: ~( v
if(moneySpace.getObjectAt(x,y)!= null){0 B* X; R# {6 y" z/ z
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
: _" k; Y% L8 v5 U' B! l+ G" | }
i& l4 d$ v" h/ W7 ]) b else{* u# Q- v6 b1 v' X$ w l; O# D
I = 0;! {3 ?* X3 Q, g9 e* Y" k8 Q# |- O
}
6 p) |/ Y1 @" ~9 K; B, m& M* J // Replace the Integer object with another one with the new value
2 a" y) ~8 w8 A! w. A V5 L' a0 d moneySpace.putObjectAt(x,y,new Integer(I + 1));0 S9 L0 ^( W& Y; g! u1 z$ b' v
}/ y9 y& L: T z- c: f
这里面if(moneySpace.getObjectAt(x,y)!= null){5 X8 A% [) n% d, a, q: t5 t) p
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();# v) G& P K. O% g4 t6 l9 V9 b
}
" y" C9 b3 e4 ]* v8 c- z2 u6 } else{
" e0 R) g4 r2 f# I I = 0;
3 _4 _7 h1 x w7 N& b0 n# Z% I0 s1 U是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?! u! h3 `7 q/ C5 x& E9 ^
初学者,还请见谅! |
|