|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
7 T! ?7 K( B5 xpublic void spreadMoney(int money){
5 x: P! Q7 Y1 u# _4 I1 H/ f // Randomly place money in moneySpace$ Y( {! C+ Q7 h# m$ m% s* ]
for(int i = 0; i < money; i++){5 Z6 T" I) s5 {" K8 w# j! @
2 W1 }; m" g3 z8 k; U
// Choose coordinates7 u; U+ C( v' `
int x = (int)(Math.random()*(moneySpace.getSizeX()));# o6 e7 Q" Z1 C, e4 B8 O
int y = (int)(Math.random()*(moneySpace.getSizeY()));
* }# `$ f% z. i5 S+ \ `/ L& i( ]' [# }
// Get the value of the object at those coordinates
0 W/ V" u. B0 I. z int I;: ]. m/ Y: I8 Z. [
if(moneySpace.getObjectAt(x,y)!= null){( u; F% i( d: P7 h! h
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
9 m9 o* T8 x9 _% d; M- z }
8 l- t) p8 W' j% z/ O else{, @0 N2 Y5 @& l' c. C
I = 0;) x7 U* k( U) ?) Z0 a& @
}
5 h2 b5 r3 }( l8 t: t8 b // Replace the Integer object with another one with the new value
7 i& Y1 w2 A- [- S) Q5 U9 F8 { v/ G moneySpace.putObjectAt(x,y,new Integer(I + 1));
8 q2 `' M1 F2 u2 F }0 ?) A' w8 L5 m
这里面if(moneySpace.getObjectAt(x,y)!= null){
0 e; E$ k; e- `' y" t! V I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
% w8 P4 v8 k" u; v( J* b% I3 ^ }' G- e7 t+ g' ]* S3 m& K% e
else{
1 ]3 [7 `8 [; d j0 o I = 0;- t5 R& k% `' r0 b/ [* X7 i
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
6 w" I# @* ^4 c) H: _' B8 [初学者,还请见谅! |
|