|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中4 N* f) ^2 O7 ^7 i6 ^0 B2 a
public void spreadMoney(int money){) p) [7 h' {# L0 ^
// Randomly place money in moneySpace- w* b: B7 u/ {/ w2 P2 N R
for(int i = 0; i < money; i++){
. x* f9 w( ? g7 Z9 M2 t% T( }( o
+ I O+ V1 \, D // Choose coordinates- s" @! G; d, q% ?9 T, N) q
int x = (int)(Math.random()*(moneySpace.getSizeX()));
1 O: h) ~1 r, `0 \ int y = (int)(Math.random()*(moneySpace.getSizeY()));
; O9 D( Z- @# x5 Q2 [
! y2 m. J# M4 Q+ F9 _; i4 R // Get the value of the object at those coordinates
/ B1 P A% Q q. T6 T( l% | int I;% ?* }* z! p4 G, X/ c
if(moneySpace.getObjectAt(x,y)!= null){
+ l, O: q+ q) J* ^! b3 X3 s I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();1 j/ L3 n N5 a% g m' m9 S
}
5 l; h2 U; ~) n# W else{
( _3 C8 u' c5 [% ]! ^2 B( ^) p( h I = 0;% w/ |% f* D9 e1 _! `
}" S9 s; Z }" Y/ H
// Replace the Integer object with another one with the new value
* Z0 n8 ]% ~/ G n, B8 { moneySpace.putObjectAt(x,y,new Integer(I + 1));
: X/ S: h/ k4 K/ e0 i( P2 F7 v3 n. m }, B4 `9 O$ `# L3 x$ w8 b, E
这里面if(moneySpace.getObjectAt(x,y)!= null){
I U9 p* k2 Y$ f' ` I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();( n' z+ Q/ e7 F( S) H
}
& ^$ @1 i9 Q7 I) |- i$ ]% L else{9 C: D3 N4 s' `, O+ Q! p
I = 0;# |; v6 \) n$ ]$ S4 \% {
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?; r: n, m5 {# y( ~( G1 E
初学者,还请见谅! |
|