|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中5 T1 u4 W. G4 E7 O4 ]( v8 |; R# t
public void spreadMoney(int money){
" c$ g. O L* V // Randomly place money in moneySpace
! n8 ~9 f; I# s" B$ d for(int i = 0; i < money; i++){
V$ O$ I; ]2 y
7 R# C# {! I' K5 O" J // Choose coordinates: l8 t9 y" d& F7 s
int x = (int)(Math.random()*(moneySpace.getSizeX()));0 _% S8 y2 X6 t
int y = (int)(Math.random()*(moneySpace.getSizeY()));; P$ Y' Q# k7 Y% b
! Q( F7 E0 K6 v7 T
// Get the value of the object at those coordinates
* L9 z ]1 m: K2 O int I;
5 b" E9 g3 ~: V( g if(moneySpace.getObjectAt(x,y)!= null){* ~* c% F! J9 F4 f+ m
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
1 _5 y, `- v1 h! e; ]( a' P }; {: |2 l. @3 @
else{
" N7 k( d# ? Q( Z3 z I = 0;
) `9 G* b3 B, [2 n( t5 e8 c! M }
6 Z* y- ] v8 O+ D _) u // Replace the Integer object with another one with the new value0 W8 w% K" p7 d2 K
moneySpace.putObjectAt(x,y,new Integer(I + 1));
6 `% a) W4 f& j: | m- h; k }7 P2 }: ]' S4 B; W
这里面if(moneySpace.getObjectAt(x,y)!= null){3 G1 i" i5 X, g
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();4 C2 u. e y$ G
}2 Z7 }" R8 w% t; n0 g X
else{
7 D7 n* u/ ]3 Z7 M* l7 y I = 0;
3 F& E$ A Y8 x& a& B+ |* _$ @( c是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?% s* W' T R U; O3 S
初学者,还请见谅! |
|