如何减少碰撞机会??
如实现100% 不会碰撞??
import java.util.*;class  t1
{
public static void main(String[] args) 
{
Object[][][] ojb1 = new Object[10][5][9];
Object[][][] ojb2 = new Object[7][5][9];
Object[][][] ojb3 = new Object[6][5][9];
int total = 0;
int count = 0; for(int i=0; i<104; i++ ){ while(count < 100){
int x = (int) (Math.random() * 5);
int y = (int) (Math.random() * 9);
int z = (int) (Math.random() * 10);
int m = (int) (Math.random() * 7);
int n = (int) (Math.random() * 6);

if(ojb1[z][x][y] == null && ojb1[m][x][y] == null && ojb3[n][x][y] == null){
ojb1[z][x][y] = new String("No Empty");
ojb1[m][x][y] = new String("No Empty");
ojb1[n][x][y] = new String("No Empty");
System.out.println("Added "  + i);
break;
}else{
count+=1;
}
}
if(count >=100){
System.out.println("Conflict "  + i);
total +=1;
} } System.out.println("Total of conflict: " + total);
}
}

解决方案 »

  1.   

    random 不保证这个,毕竟是随机的。呵呵!
      

  2.   


    Object[][][] ojb1 = new Object[n][5][9]; 
    Object[][][] ojb1 = new Object[x][5][9]; 
    Object[][][] ojb1 = new Object[y][5][9]; 3 者位置[5][9]同时都是 null..就return true.. 例如: 
    ojb1 [1][2][3] = null 
    obj2 [5][2][3] = null 
    obj3 [3][2][3] = null 
    这时就 return true; 当3个ojbect 一边存入数据..一边计算剩下多少个同时共有的空位..(希望这个)
      

  3.   

    Random 的数字, 我希望做到好像有一幅地图一样….知道哪个位置已经有对象..不会撞向它
      

  4.   

    我这里没有碰撞,代码有问题:
    if(ojb1[z][x][y] == null && ojb1[m][x][y] == null && ojb3[n][x][y] == null)
    的第2个判断应该是ojb2[m][x][y] == null 
      

  5.   

    给你个思路吧。
    在a, b, c, d的范围内随即取值,不能重复(碰撞?)。
    x[0]=a
    x[1]=b
    x[2]=c
    x[3]=dRandom rand = new Random();
    int temp;
    for (int j = 4; j > 0; j--) {
        int i = rand.nextInt(j);
        System.out.println(x[i]);
        temp = x[i];
        x[i] = x[4 - j];
        x[4 - j] = temp;
    }大意就是这样
      

  6.   

    晕,错了。
    应该将x[i]与x[j]互换。以保证取过的值在数组的最后,再限制随机数的取值范围,则避免重复。