|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
$ L0 t& w9 m& s" v6 ^( T1 |) N! Ppublic void spreadMoney(int money){
6 ?+ v# i: w" o! w2 e // Randomly place money in moneySpace
* }/ A, `8 a9 c for(int i = 0; i < money; i++){* S" G0 _& Y3 E
' M" C8 ^; F# Y' g Y2 V5 y# P9 I
// Choose coordinates
, _7 `% j: r8 V* p6 } int x = (int)(Math.random()*(moneySpace.getSizeX()));. K$ B/ z( G v. B" K& z: J* P
int y = (int)(Math.random()*(moneySpace.getSizeY()));
% v; r/ s4 y& a) P* x) p
+ v C0 M* }- r: ^8 E // Get the value of the object at those coordinates
% Y6 N# B6 G8 f. Z& G8 n5 A+ Z int I;; Z; W, \ A( w3 @" o2 Y
if(moneySpace.getObjectAt(x,y)!= null){
) d4 |+ a& x( S/ `1 n I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();6 V9 R$ e4 R: }
}+ u2 ?5 ?* r' F, c
else{
2 c, y2 L$ G9 N$ z6 I1 H I = 0;- \2 ^, J( p8 U% n2 L
}3 J+ I1 v1 I1 B$ Y' X' x
// Replace the Integer object with another one with the new value! S# P5 G* ?/ O" |! h
moneySpace.putObjectAt(x,y,new Integer(I + 1));
) C9 |5 z% j6 {0 o0 H }9 D6 w- l; W5 u5 b( D$ b2 S! _4 O
这里面if(moneySpace.getObjectAt(x,y)!= null){
2 o @. U) s* P5 y4 C3 p5 m2 v I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
8 j. p3 o: h' a. L: N% ?3 I }( D% A; \( @9 H3 J4 E. f9 @
else{
& a8 o8 C9 x, n+ {* ~- S I = 0;$ p/ H. L; _, `! I# X, y$ y
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?: Q4 D& J( ?& e
初学者,还请见谅! |
|