|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中0 h& h* ~# U3 O
public void spreadMoney(int money){2 n2 O6 q( l" y# I8 A& n( M( a
// Randomly place money in moneySpace
- P/ y: o4 R6 G% C3 g9 _ for(int i = 0; i < money; i++){
, y- _! u; g# M! C% H
5 w4 B" m. X1 G! M) n7 v // Choose coordinates
, J; V( x" O, p4 L int x = (int)(Math.random()*(moneySpace.getSizeX()));
* l8 x% h9 r. s int y = (int)(Math.random()*(moneySpace.getSizeY()));
8 o; ^2 ]* u/ A0 e6 d7 G8 \7 F7 H* o0 x; x5 T
// Get the value of the object at those coordinates
% a) z5 _" d1 ~1 @0 B int I;6 p6 [$ ~, @. P2 ^: l7 N1 `
if(moneySpace.getObjectAt(x,y)!= null){. _. G: B& G( R7 ` P
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
8 f, z! U$ u& k }- F- T2 l# g- d9 S" ~
else{
^: B5 S g5 q7 s% d I = 0;6 L3 A- r7 o' `$ a* Q5 V0 f" T
}
" [, E( d Y. w/ z: p // Replace the Integer object with another one with the new value
# S8 a: ?+ R9 b2 k* r$ _7 X moneySpace.putObjectAt(x,y,new Integer(I + 1));
( p* z7 o' ~. t0 Z7 G }
, c# g# X" S4 \这里面if(moneySpace.getObjectAt(x,y)!= null){
% b6 }+ v# A% X3 v% _; [8 o/ b I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();- G1 f- r8 k8 o2 | H6 ^: _
}! e1 W/ r- I, W- C( ?8 _/ A, D2 N- i
else{
- {! o* |+ a/ ]# D. u0 H I = 0; l" w* y" Y. e9 t5 y- F
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
C4 I7 p1 | F初学者,还请见谅! |
|