0

我正在尝试创建一种方法来检查某些 Y 坐标的第一个差异,如果第一个差异相同,它将采用这些差异并检查第二个差异,并且我想继续检查直到我的变量相等。我的问题是检查我的差异数组中的相等性

public static double[] Difference(double ardY[]) {
    double ardDifference[] = new double[5];
    for (int j = 0; j < 5; j++) {
        ardDifference[j] = ardY[j + 1] - ardY[j];
        while (ardDifference[j] != ardDifference[j]) {
            for (int i = 0; i < 4; i++) {
                ardDifference[i] = ardY[i + 1] - ardY[i];
                if (ardDifference[j] == ardDifference[i]) {
                    return ardDifference;
                    break;
                }
            }
        }
    }
    return ardDifference;
}
4

1 回答 1

1

我不太确定您要在这里做什么,但是如果您想计算差异,直到获得一系列都相等的差异,那么您可以使用递归很容易地做到这一点:

public static double[] Difference(double ardY[]) {
    double ardDifference[] = new double[ardY.length - 1];

    boolean allEqual = true;
    double prev;
    for (int j = 0; j < ardDifference.length; j++) {
        ardDifference[j] = ardY[j + 1] - ardY[j];
        if (j != 0) {
            allEqual &= prev == ardDifference[j];
        }
        prev = ardDifference[j];
    }

    if (allEqual) {
        return ardDifference;
    } else {
        return Difference(ardDifference);
    }
}
于 2013-10-01T13:03:08.067 回答