簡體   English   中英

比較兩個double arrays,然后打印差數+最大差

[英]comparing two double arrays, and then printing the number of differences + largest difference

我有兩個具有雙精度值的 arrays。

double[] testArray1 = {3.3, 2.7, 6.4, 1.8, 9.5, 1.4, 9.0, 7.0, 6.5, 3.7};
double[] testArray2 = {5.6, 4.7, 2.8, 3.7, 5.8, 2.7, 6.4, 1.8, 9.5,10.2};

我需要比較這些值,然后打印差異的數量以及最高差異。 到目前為止,我已經做到了;

    double nDiff = 0;
    double BiggestDiff = 0;
    double currentDiff = 0;

    for (int i = 0; i < testArray1.length; i++){
      for (int k = 0; k < testArray1[i].length; k++){
        if (testArray1[i][k] != testArray2[i][k]) {
          nDiff +=;
          currentDiff = 0;

          if (testArray1[i][k] > testArray2[i][k]) {
            nDiff = (testArray1[i][k] - testArray2[i][k]);
          }
          else {
            currentDiff = (testArray2[i][k] - testArray1[i][k]);
          }
          if (currentDiff > BiggestDiff) {
            BiggestDiff = currentDiff;
          }
        }
      }
    }
    if (nDiff > 0) {
      System.out.println("Number of differences was " + nDiff);
      System.out.println("The biggest difference was " + BiggestDiff);
    }

首先,我得到一個指向;illegal start of expression錯誤。 nDiff +=; . 所以我刪除它只是為了得到幾個錯誤告訴我double 不能被取消引用並且需要數組,但是 double found這讓我覺得我完全錯誤地完成了代碼。

提前致謝。

  1. 首先: for (int k = 0; k < testArray1[i].length; k++){是錯誤的。 當您調用 testArray1[i] 時,它返回雙數而不是數組,您無法獲得單個雙數的長度屬性。

  2. Next: testArray1[i][k] != testArray2[i][k]這些 arrays 不是矩陣,所以這里不能有雙括號,比如 [][],如果你想從數組中取出元素,它應該只是 testArray [一世]。

  3. nDiff +=; 加上什么? 你必須在 += 之后放一些東西,比如nDiff += k;

  4. 您想比較第一個元素與第一個元素、2v2、3v3 等? 或者第一個數組中的每個元素與第二個數組中的每個元素?

如果每個 x every 那么你的代碼應該是這樣的:

        double[] testArray1 = {3.3, 2.7, 6.4, 1.8, 9.5, 1.4, 9.0, 7.0, 6.5, 3.7};
        double[] testArray2 = {5.6, 4.7, 2.8, 3.7, 5.8, 2.7, 6.4, 1.8, 9.5, 10.2};

        double biggestDifference = 0;
        double differenceCounter = 0;
        double currentDifference;

        double firstArrayValue;
        double secondArrayValue;

        for (int i = 0; i < testArray1.length; i++){
            for (int k = 0; k < testArray1.length; k++){
                firstArrayValue = testArray1[i];
                secondArrayValue = testArray2[k];
                if (firstArrayValue != secondArrayValue) {
                    differenceCounter += 1;
                    currentDifference = Math.abs(firstArrayValue - secondArrayValue);
                    if (currentDifference > biggestDifference) {
                        biggestDifference = currentDifference;
                    }
                }
            }
        }
        if (differenceCounter > 0) {
            System.out.println("Number of differences was " + differenceCounter);
            System.out.println("The biggest difference was " + biggestDifference);
        }

然后它會打印:

Number of differences was 95.0
The biggest difference was 8.799999999999999

如果你想比較 1v1、2v2、3v3 等,你可以假設兩個 arrays 具有相同的長度,那么你應該使用一個索引,如:

        double[] testArray1 = {3.3, 2.7, 6.4, 1.8, 9.5, 1.4, 9.0, 7.0, 6.5, 3.7};
        double[] testArray2 = {5.6, 4.7, 2.8, 3.7, 5.8, 2.7, 6.4, 1.8, 9.5, 10.2};

        double biggestDifference = 0;
        double differenceCounter = 0;
        double currentDifference;

        double firstArrayValue;
        double secondArrayValue;

        if (testArray1.length == testArray2.length) {
            for (int i = 0; i < testArray1.length; i++) {
                    firstArrayValue = testArray1[i];
                    secondArrayValue = testArray2[i];
                    if (firstArrayValue != secondArrayValue) {
                        differenceCounter += 1;
                        currentDifference = Math.abs(firstArrayValue - secondArrayValue);
                        if (currentDifference > biggestDifference) {
                            biggestDifference = currentDifference;
                        }
                    }
            }
            if (differenceCounter > 0) {
                System.out.println("Number of differences was " + differenceCounter);
                System.out.println("The biggest difference was " + biggestDifference);
            }
        } else {
            throw new IllegalArgumentException("Sorry, arrays have different length :(");
        }

然后它會打印:

Number of differences was 10.0
The biggest difference was 6.499999999999999

你應該使用正常的 IDE 像 InteliJ 或 Eclipse,它會告訴你編譯時錯誤。 干杯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM