Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.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 在while循环中是否可能存在数量可变的条件?_Java_Android_Loops_For Loop_While Loop - Fatal编程技术网

Java 在while循环中是否可能存在数量可变的条件?

Java 在while循环中是否可能存在数量可变的条件?,java,android,loops,for-loop,while-loop,Java,Android,Loops,For Loop,While Loop,我尝试将对象放置到屏幕上,在放置每个对象后,我使用while循环不断替换下一个对象,直到它与之前的任何对象都不重叠,然后如果尝试次数过多,我将停止在那里绘制 问题是在放置每个对象后while循环中的条件数量需要增加,所以我想知道是否有某种方法,类似于使用求和而不是加法 编辑: 对于小于最大对象数的每个i,所有这些代码都包含在另一个for循环中。我尝试引入你可以看到的for循环,但这只会检查对象是否与另一个对象相交,以便它可以与前一个对象重叠 if(i > 0) {

我尝试将对象放置到屏幕上,在放置每个对象后,我使用while循环不断替换下一个对象,直到它与之前的任何对象都不重叠,然后如果尝试次数过多,我将停止在那里绘制

问题是在放置每个对象后while循环中的条件数量需要增加,所以我想知道是否有某种方法,类似于使用求和而不是加法

编辑:

对于小于最大对象数的每个
i
,所有这些代码都包含在另一个for循环中。我尝试引入你可以看到的for循环,但这只会检查对象是否与另一个对象相交,以便它可以与前一个对象重叠

            if(i > 0) {
                for (int j = i - 1; j >= 0; j--) {
                    while ((atomXPosition[i] < atomXPosition[j] + atomWidth[j]
                            && atomXPosition[i] + atomWidth[i] > atomXPosition[j]
                            && atomYPosition[i] + atomWidth[i] > atomYPosition[j]
                            && atomYPosition[i] < atomYPosition[j] + atomWidth[j])) {
                        atomXPosition[i] = (float) Math.random() * (screenWidth - atomWidth[i]);
                    }
                }
            }
if(i>0){
对于(int j=i-1;j>=0;j--){
而((atomXPosition[i]atomXPosition[j]
&&atomYPosition[i]+atomWidth[i]>atomYPosition[j]
&&atomYPosition[i]
您可以在
while
循环中使用函数作为条件。此函数可以将类似于
ArrayList
的内容作为输入,您可以在
while
循环的每次迭代中将对象添加到
ArrayList

为什么不使用Atom类

import java.util.ArrayList;
import java.util.List;

public class Atom {

    private static final int NUM_ATOMS = 20;
    private static final int MAX_TRIES = 3;

    public static void main(String[] args) {
        List<Atom> atoms = new ArrayList<>();

        for (int i = 0; i < NUM_ATOMS; i++) {
            Atom atom = new Atom();

            atoms.add(atom);

            for (int tries = 0; tries < MAX_TRIES; tries++) {
                if (atom.intersectsAny(atoms)) {
                    int randomX = //...
                    int randomY = //...
                    atom.moveTo(randomX, randomY);
                }
            }
        }
    }

    private int x;
    private int y;
    private int width;
    private int height;

    public boolean intersectsAny(List<Atom> atoms) {
        return atoms.stream().anyMatch(this::intersects);
    }

    private boolean intersects(Atom other) {
        if (this == other) {
            return false;
        }

        return //... check if this atom intersects other atom
    }

    public void moveTo(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
import java.util.ArrayList;
导入java.util.List;
公共类原子{
私有静态最终int NUM_ATOMS=20;
私有静态最终整数最大值=3;
公共静态void main(字符串[]args){
列表原子=新的ArrayList();
对于(int i=0;i
很难将其可视化。你能给我们看一些你目前得到的代码吗?while循环的条件似乎并不一致。目前还不清楚您试图实现的目标。需要在0和已放置对象的数量之间对j重复while循环中的所有条件