|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
: T. Q% C" b( x+ r- k$ Apublic void spreadMoney(int money){9 @/ u# g; M. T# R7 p7 N5 O# Z
// Randomly place money in moneySpace
" Q* G( }4 R- P4 z3 l; R9 a for(int i = 0; i < money; i++){
4 T X0 c e9 z) t2 c2 v/ i6 U$ G7 G6 ?/ r
// Choose coordinates% Z# T" h* w9 a M
int x = (int)(Math.random()*(moneySpace.getSizeX()));
" N' f8 e! |% `' K. g% N* V+ h int y = (int)(Math.random()*(moneySpace.getSizeY()));
! t: } b- r& V, U
N% G4 I# _9 [7 e; e# Z4 r( l // Get the value of the object at those coordinates7 x8 z: F1 F6 u) U: A
int I;4 T) Q O# f1 z. v; D( ~
if(moneySpace.getObjectAt(x,y)!= null){
5 t8 a. Q. S, H4 ~/ h& a' B I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();3 ~: M/ h, Z2 Z( F
}
+ H7 h2 v7 Q( F6 S2 ~, S2 c5 _ else{
% W: ^1 O) b7 Y: V a3 z2 N9 m I = 0;1 Q: C' E3 N4 A; F0 L; H8 ?
}
6 `# T, w, V! P; m; e9 W // Replace the Integer object with another one with the new value0 U4 M( X; A( [( f8 R9 B
moneySpace.putObjectAt(x,y,new Integer(I + 1));
9 t1 X% v2 p) R8 ^& x }* {* F& k1 `5 f$ T: k5 h
这里面if(moneySpace.getObjectAt(x,y)!= null){8 R# f) a3 s/ j3 T
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();* g) [& s- y8 i& Y) B5 A/ Z
}
$ t, s5 b+ ?* D8 P, [" L) C1 K else{
0 f K! g+ E7 ?7 X1 }/ F7 }2 u7 q I = 0;
+ A4 u& a% F. _% w1 R% c/ M是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?, p# E" N8 |( z& i2 @( |
初学者,还请见谅! |
|