Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 随机浮点值正在重复_Java - Fatal编程技术网

Java 随机浮点值正在重复

Java 随机浮点值正在重复,java,Java,我试图让我的代码重新创建一个随机的x和y值,如果已经有另一个对象占用了所选的值。但出于某种原因,循环只是不断地给我相同的浮点值,导致它永远循环,而不是创建新的值,然后对照以前的x和y变量检查该值。这是密码。如果你还需要,请告诉我 static int numberOfDots = 5; static int windowSize = 400; float[] pos = new float[2]; float[] vel = new float[2]; float[] acc = new fl

我试图让我的代码重新创建一个随机的x和y值,如果已经有另一个对象占用了所选的值。但出于某种原因,循环只是不断地给我相同的浮点值,导致它永远循环,而不是创建新的值,然后对照以前的x和y变量检查该值。这是密码。如果你还需要,请告诉我

static int numberOfDots = 5;
static int windowSize = 400;

float[] pos = new float[2];
float[] vel = new float[2];
float[] acc = new float[2];
float dotSize = 0;
float dotMass = 0;
float dotWidth = 0;
float dotRadius = 0;
float[] centrePos = new float[2];

float[][] dots = new float[numberOfDots][10];

//for making dots
boolean first = true;
boolean creationCollision = false;
boolean dotCollision = false;

// pos means x and y positions
// vel means x and y velocity
// centrePos means centre of the dot x and y
// dotMass isnt implemented yet
    public boolean collisionOnCreate() {
        //compare all dots
        for (int i = 0; i < dots.length; i++) {
            //against any dots later in the array
            // since they will already have compared themselves to dots previous in the array
            // - dont need to loop over them again
            for (int a = i + 1; a < dots.length; a++) {
                if (dots[a][0] != 0) {
                    creationCollision = dots[i][1] + dots[i][8] > dots[a][1] && dots[i][1] + dots[i][8] < dots[a][1] + dots[a][8] || dots[i][1] > dots[a][1] && dots[i][1] < dots[a][1] + dots[a][8] || dots[i][0] > dots[a][0] && dots[i][0] < dots[a][0] + dots[a][8] || dots[i][0] + dots[i][8] > dots[a][0] && dots[i][0] + dots[i][8] < dots[a][0] + dots[a][8];
                    System.out.println(dots[a][0] + " " + dots[a][1]);
                }
                //check if the left-most part of the dot is anywhere between the leftmost and right most part of another dot
                //check if the rightmost part of the dot is anywhere between the left most and right most part of another dot
                //check if the top part of the dot is anywhere between the top and bottom part of another dot
                //check if the bottom of the dot is anywhere between the top and bottom part of another dot
                // I know its long but its easier then running a bunch of if statements to do the same task    
            }
        }
        return creationCollision;
    }

    public void createDots(int i) {
        pos[0] = r.nextFloat() * 300;
        pos[1] = r.nextFloat() * 300;
        dotWidth = r.nextFloat() * 30;
        if (dotWidth < 5) {
            dotWidth *= 10;
        }
        dotRadius = dotWidth / 2;
        dotMass = r.nextFloat() / 10;
        centrePos[0] = centrePos[0] + dotRadius;
        centrePos[1] = pos[1] + dotRadius;
        vel[0] = r.nextFloat() / 10;
        vel[1] = r.nextFloat() / 10;
        check(i);
    }

    public void check(int i) {
        collisionOnCreate();
        if (creationCollision) {
            createDots(i);
            System.out.println("collision on creation");
        } else {
            setValues(i);
            System.out.println("dot number " + i + " is created");
        }
    }

    public void setValues(int i) {
        dots[i][0] = pos[0];
        dots[i][1] = pos[1];
        dots[i][2] = vel[0];
        dots[i][3] = vel[1];
        dots[i][4] = dotRadius;
        dots[i][5] = centrePos[0];
        dots[i][6] = centrePos[1];
        dots[i][7] = dotMass;
        dots[i][8] = dotWidth;
    }

    //create an array of dots and assign them values for x, y, radius, etc.
    public float[][] Dots() {
        if (first == true) {
            for (int i = 0; i < numberOfDots; i++) {
                    createDots(i);
            }
            first = false;
        } else {
            for (int i = 0; i < numberOfDots; i++) {
                // update values
                dots[i][0] = dots[i][0] + dots[i][2];
                dots[i][1] = dots[i][1] + dots[i][3];
                centrePos[0] = dots[i][0] + dots[i][4];
                centrePos[1] = dots[i][1] + dots[i][4];
                dots[i][5] = centrePos[0];
                dots[i][6] = centrePos[1];
                collisionOnWall(i);
            }
        }
        repaint();
        return dots;
    }
static int numberOfDots=5;
静态int windowSize=400;
浮动[]位置=新浮动[2];
浮动[]水平=新浮动[2];
浮动[]acc=新浮动[2];
浮动点大小=0;
浮点数质量=0;
浮动点宽度=0;
浮点数半径=0;
float[]centrePos=新的float[2];
浮点[][]点=新浮点[numberOfDots][10];
//用来做圆点的
布尔值优先=真;
布尔creationCollision=false;
布尔点碰撞=假;
//pos表示x和y位置
//vel表示x和y速度
//中心点表示点x和y的中心
//dotMass尚未实现
公共布尔冲突onCreate(){
//比较所有点
对于(int i=0;i
我认为问题不在于随机数的生成。相反,创建/检查的逻辑有问题

首先,在
createDots()
中,您为一些变量赋值,这些变量表示一个新的点(为什么不为此创建一个专用类?)。然后,您不会将新点添加到
数组中,而是直接检查是否存在任何冲突

但是,您只检查
dots
数组中已经存在的值,而不考虑新创建的值


另一件事,
creationCollision
条件对我来说真的是不可读的。为了清楚起见,你应该用一些括号。另外,要确保它确实在做你想做的事情。

我认为问题不在于随机数的生成。相反,创建/检查的逻辑有问题

首先,在
createDots()
中,您为一些变量赋值,这些变量表示一个新的点(为什么不为此创建一个专用类?)。然后,您不会将新点添加到
数组中,而是直接检查是否存在任何冲突

但是,您只检查
dots
数组中已经存在的值,而不考虑新创建的值


另一件事,
creationCollision
条件对我来说真的是不可读的。为了清楚起见,你应该用一些括号。另外,还要确保它确实在做你想做的事情。

你似乎从来没有设置过点[
public void createDots(int i) {
    pos[0] = r.nextFloat() * 300;
    pos[1] = r.nextFloat() * 300;
    dotWidth = r.nextFloat() * 30;
    if (dotWidth < 5) {
        dotWidth *= 10;
    }
    dotRadius = dotWidth / 2;
    dotMass = r.nextFloat() / 10;
    centrePos[0] = centrePos[0] + dotRadius;
    centrePos[1] = pos[1] + dotRadius;
    vel[0] = r.nextFloat() / 10;
    vel[1] = r.nextFloat() / 10;
    check(i);
}