|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
7 c# p4 X7 |* B+ r; |$ f9 jpublic void spreadMoney(int money){
* \% V) Z h3 z8 I$ B. ^: W" L // Randomly place money in moneySpace
! {: s% T/ I6 ^9 O5 ?# B for(int i = 0; i < money; i++){
; t9 Q) W% x" ?. |: k4 }1 ? O- S* A- H4 x) Z
// Choose coordinates! l6 f0 K4 H: Y% ]; u
int x = (int)(Math.random()*(moneySpace.getSizeX()));. `/ }6 p- o/ ]. ~" m
int y = (int)(Math.random()*(moneySpace.getSizeY()));# T b* _' F- T, E1 ]$ G. K
) H' Q* p* ]1 ~, O/ e
// Get the value of the object at those coordinates& y) X% f( K2 D+ ?# O8 @( Y
int I;
/ h) |, ?+ q6 ~ |! R if(moneySpace.getObjectAt(x,y)!= null){
* { {+ c+ b4 C) s: c; x I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
0 V3 |( Q" `0 O4 Z% s }( }$ P( g% P# P, `0 _! d
else{. \; Z: s4 q/ b/ o
I = 0;
% b" Q; Z7 n/ e5 E" B$ ]1 A3 K% Q }
# E9 u1 N! @6 t* j( N' n // Replace the Integer object with another one with the new value6 I+ ^. p/ K8 e8 u
moneySpace.putObjectAt(x,y,new Integer(I + 1));9 z+ F* G6 R3 J; c$ \1 a2 S
}) F! x) A) t( \. [: c5 Y: J
这里面if(moneySpace.getObjectAt(x,y)!= null){. `; B; p5 Z3 a
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
& m& u0 g, a. t6 o* F7 @# V( I, p }; ~9 O I& x' S! ^- A
else{
9 r8 j X( U- q" I5 C5 ~, q I = 0;0 h" _: A" {( h4 [/ E
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?, D) n/ j0 A2 e5 _9 Q s
初学者,还请见谅! |
|