簡體   English   中英

JAVA:我做錯了什么?

[英]JAVA: What am I doing wrong?

我正在嘗試確定兩個整數的 arrays 之間的漢明距離。 前:aList,= null,bList.= null。 aList;長度 == bList:長度。 發布,返回整數的兩個 arrays 之間的漢明距離。 我不確定我做錯了什么。 我剛剛開始學習如何編碼:感謝任何幫助。 提前致謝 :)

這是我的代碼:

public class test {

public static int hammingDistance(int[] aList, int[] bList) {
    // check preconditions
    if (aList == null || bList == null || aList.length != bList.length)
        throw new IllegalArgumentException("Violation of precondition: " +
            "hammingDistance. neither parameter may equal null, arrays" +
            " must be equal length.");
    //Starting a counter
    int counter = 0;
    System.out.println("test");
    //checking to see if there is a mismatch in the values of the two given arrays
    for (int i = 0; i < bList.length; i++) {
        if (bList[i] != aList[i]) {
            //increasing counter everytime there is a mismatch
            counter++;
        }
    }
    return counter;
}

public static void main(String[] args) {
    int[] aList = { 1,3,3,4 };
    int[] bList = { 1,2,10,4 };
    System.out.println(hammingDistance(aList, bList));
}

}

更新代碼

只要有匹配項,您就將計數器重置為零。 所以反擊永遠無效。 刪除代碼的 else 部分。

當兩個元素相等時,您正在重置counter (請參閱您的else語句)。 當兩個被叫相等時,您不應該做任何事情。

希望能幫助到你,

暫無
暫無

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

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