|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中* t' Q8 J5 p. Z0 A( E7 n7 c" X. [) q
public void spreadMoney(int money){
/ \7 ?5 J1 |. E- [! H: {2 c- j4 r // Randomly place money in moneySpace
' a ?+ U0 K' l- e9 p; N9 ]" P for(int i = 0; i < money; i++){
, i' ?$ u* _) x4 x$ L# M; a! A$ q' Z( t- c1 Y
// Choose coordinates
% Y0 Z1 ~0 T8 g7 J! B& o# L int x = (int)(Math.random()*(moneySpace.getSizeX()));
$ |; T/ ^, i" d" l! @: R, H int y = (int)(Math.random()*(moneySpace.getSizeY()));
0 [( c' j, l/ s, X" r6 Q' J& d# r/ k4 k- l3 U3 r6 H
// Get the value of the object at those coordinates- o( `# @4 j6 M0 k
int I;$ x2 ~( d6 e2 O- a/ \( e) I5 p
if(moneySpace.getObjectAt(x,y)!= null){9 s, ^3 C( e; l5 s9 C
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
/ M i. Z7 Q5 t& l& \- A' @ }
7 \% E' l @: E* M. a' V! K else{$ Q% D3 |" R/ j# k- m' W+ S
I = 0;
6 E0 ^/ A0 M- N$ P0 q" a) t$ `' t }
2 z x `5 s8 W9 T9 H2 d* D6 J // Replace the Integer object with another one with the new value( u0 N5 R9 i3 A# y) A7 \0 x1 C
moneySpace.putObjectAt(x,y,new Integer(I + 1));
! z2 {7 N) V/ E0 s9 Y3 L }
& @! ?9 e: _ N9 H' O这里面if(moneySpace.getObjectAt(x,y)!= null){
% a2 A* B5 T! y7 ` I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
! `2 D, R! g. G, H% i8 m- c }
: Y4 a9 }1 O- | else{
6 L- a9 Z* f# q ?& b2 R I = 0;% n w5 e! [* X: m
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?4 ?- z$ d8 a1 n M7 }4 R
初学者,还请见谅! |
|