簡體   English   中英

Java:將二維數組轉換為具有組織和隨機數的一維數組

[英]Java: 2D array into 1D array with organisation and random numbers

我想總結每個參賽者從每個評委那里得到的分數(這是存儲在一個二維數組中),我想把它總結成一維數組。 例如:

參賽者1:

裁判1:5分

裁判2:3分

該方法應該這樣做:array[0] = 5+3

這是我創建的,但是如果judge大於contestant則它不起作用,我也不相信它正常工作。 我試過調試但沒有進一步成功。 allPoints 是包含所有信息的二維數組; 例如, allPoints[0][0]judge 1contestant 1

有任何想法嗎?

int output = 0;
        for (int t = 0; t < judge; t++) {
            for(int i = 0; i < contestant; i++) {
                output += allPoints[t][i];
            }
            Sum[t] = output;
            output = 0;
        }

可能你混淆了循環。 如果我完全猜到您的問題,那么解決方案將是

    int output = 0;
    for(int i=0; i<contestant; i++){
        for(int t=0; t<judge; t++){
            output+= allPoints[t][i];  //It has a chance to be  allPoints[i][t] according how  you generate allPoints
        }
        Sum[i] = output;
        output = 0;
    }

對於每位contestant計算所有judge給出的總分並將其放入Sum 所以Sum持有每個contestant總分。

如果 allPoints[i][j],如果 i 是裁判,j 是參賽者。 您需要驗證這一點。 正確的代碼是:

int output = 0;
for (int j = 0; j < contestant; j++) {
    for(int i = 0; i < judge; i++) {
        output += allPoints[i][j];
    }
    Sum[j] = output;
    output = 0;
}

基本上,對於每位參賽者,您需要總結每位評委給出的分數。 你現在所做的是為每個評委總結他給所有參賽者的分數。

暫無
暫無

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

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