|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
5 ^6 @# l. @: e: xpublic void spreadMoney(int money){
7 v |# X' j c" C+ i { // Randomly place money in moneySpace$ e' P$ ]5 C/ M0 v6 j- f4 [! b0 q' y& E
for(int i = 0; i < money; i++){# `# z F) @# [3 z" A9 E" a
$ K. X' n, b9 T1 T
// Choose coordinates
; Y( V& E& N7 ^ t( t int x = (int)(Math.random()*(moneySpace.getSizeX()));
% a+ P+ L* @2 a; Z int y = (int)(Math.random()*(moneySpace.getSizeY()));+ j9 j. {: s& C N0 G4 s5 z
; K# e: f* e) H: I( l2 _" p // Get the value of the object at those coordinates
2 t3 w0 T4 {( T4 c% X int I;
+ X! V5 N$ r: ^ if(moneySpace.getObjectAt(x,y)!= null){
- B; |2 v( `0 Y# L I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
% c% L9 T1 ?+ K6 j5 [8 @ }
4 J$ Y& m1 G: R5 P1 | else{
& b7 H' [( E; [3 n8 K) n I = 0;, X/ ~ Y4 V1 a0 D. j' I9 `
}
' c( u# i d# u# K1 p5 Q // Replace the Integer object with another one with the new value0 M3 F6 m2 `. H
moneySpace.putObjectAt(x,y,new Integer(I + 1));
) X/ ]/ U2 y# R% C& {$ K5 e- A }
- U4 W7 Z! r9 {9 ?. \# p% k% Z/ N这里面if(moneySpace.getObjectAt(x,y)!= null){: ^9 x1 h& Y4 K5 h# Y$ C
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();4 b2 u# I; m. B6 l' S$ W9 ]& H* E
}
3 R6 ]9 ?3 `( ]* I0 d( Z8 k else{
0 f5 o* W$ n5 c I = 0;( B) h3 `6 ~; p/ l! ~! l
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢? Z2 A* I. I$ v
初学者,还请见谅! |
|