簡體   English   中英

我的運動凍結了,我不明白為什么

[英]my exercise freezes and I don't understand why

我在程序查找重復號碼的地方創建了這個問題。 因為它不起作用?

package javaapplication5;

/**
 *
 * @author miste
 */
public class JavaApplication5 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        NewClass n = new NewClass();
        int[] numero = {2, 3, 4, 5, 6, 3};
        n.cerca(numero);
    }

}

package javaapplication5;

import static java.lang.reflect.Array.get;

/**
 *
 * @author miste
 */
public class NewClass {
    int [] numero = {2, 3, 4, 5, 6, 3};

    public void cerca(int[] numero) {
        int tmp = 0;
        for (int i = 0; i < 7; i++) {
            tmp = numero[i];
        }
        for (int j = 0; j < 7; j++) {
            if (numero[j] == tmp) {
                System.out.println("il duplicato è " + tmp);
            }
        }
    }
}

您的數組長度為 6。您可以迭代到的最大索引為 5。由於您從 0 到 6 對其進行迭代,因此它為您提供了一個 ArrayIndexOutOfBoundsExcption。 您可以通過將循環條件更改為i<6來糾正它

暫無
暫無

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

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