ジャワ - 7つの整数で読む、reoccurrancesのカウント数

Max091:

私は、イントロのJavaクラスを取っていると私は割り当てにこだわって。目標は、7つの整数を読み込み、単一次元配列を使用してプログラムを書くことであるし、各値入力された再発は(あなたが二度番号12を入力した場合、出力ラインの一つが、すなわち「ナンバー12が2回発生した回数を表示します。 「)

私はそれのいくつかはすでに書かれてありますが、私は、配列の各要素が発生した回数をカウントする方法でこだわっています。私は、私が法と二番目の配列を必要とするつもりだという考えを持っているが、私は壁にぶつかるました:

public class NumOfOccurrIn7 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // Declare scanner and array
        Scanner A = new Scanner(System.in);
        int counter[] = new int[7];
        System.out.print("Please enter 7 numbers: ");

        //Populate initial array
        for(int t = 0; t < 7; t++) {
            counter[t] = A.nextInt();
        }

        //Process # of reoccurences and print results
        for(int t=0; t<7; t++) {


        }


        for(int t=0; t<7; t++) {
            System.out.print("Number " + counter[t] + " occurs " + x + 
                    " times./n");
        }

    }

    public static int CountOccurrance(int counter[]) {

    }

}

ご意見ありがとうございます!

アンドレアス:

数12が二回発生した場合は、その最初の数字はすでに処理されたかどうかをチェック、出力が二回印刷しません。あなたは、配列の要素を反復することによってそれを行う前に t、そのうちの一つは、現在の数と同じである場合、あなたは何も印刷されません。

次に、あなたは、配列の残りの部分をチェックし、現在の数が発生した回数をカウントし、メッセージを印刷します。

// Process # of reoccurences and print results
for (int i = 0; i < 7; i++) {
    boolean print = true;
    int count = 0;
    for (int j = 0; j < 7; j++) {
        if (counter[j] == counter[i]) {
            if (j < i) {
                print = false;
                break;
            }
            count++;
        }
    }
    if (print) {
        System.out.println("Number " + counter[i] +
                           " occurs " + count + " times.");
    }
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=18012&siteId=1