|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中. ~; l5 q% }4 x. y( J
public void spreadMoney(int money){
0 s5 F/ c' ]1 l5 |2 \; O8 s // Randomly place money in moneySpace: ~# Z1 o% A N8 P$ P: R1 N2 D" w$ Z
for(int i = 0; i < money; i++){
# m3 R, W. n. {; F
. h, _+ O6 `( v( E' G# Y // Choose coordinates, u- Z; I1 w8 Q/ H: A, T$ W
int x = (int)(Math.random()*(moneySpace.getSizeX()));* j& `4 i5 M* ]
int y = (int)(Math.random()*(moneySpace.getSizeY()));4 j/ N, w0 m! W4 t. Z
" S/ e) `9 P+ f R; O$ }. n; H* \: Y% x' z
// Get the value of the object at those coordinates' X/ q8 p/ u U* Y J6 ?+ V( G
int I;
( f) c2 [ G, W- M4 \5 ?/ b9 D if(moneySpace.getObjectAt(x,y)!= null){, Y" x! I( ?2 L
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
8 }. R' o7 q$ O/ Y9 U }2 C- q* B( S! F' S* p/ h
else{
; F' e2 N0 I3 {& L/ z0 R. { I = 0;
; s! W9 w5 `2 ]/ Y2 X+ k }
. [/ Y) ?7 m; @% ` // Replace the Integer object with another one with the new value
6 ]+ [4 v3 l' M moneySpace.putObjectAt(x,y,new Integer(I + 1));
* `0 n4 t; ~; H }. R5 N5 R9 E, x& {
这里面if(moneySpace.getObjectAt(x,y)!= null){
' [* _- t+ I# e3 ^3 T I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
7 H% s J7 k+ n. f' L# W+ j7 ~9 e }
* R& i' a% X% I) C# R7 U else{
c6 H, W; [3 e8 l$ a! H I = 0;1 l/ E) T& U: ]) q _* ~1 [. R$ d
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?6 v. ?4 c5 H7 b: z' z. l% U5 h
初学者,还请见谅! |
|