[英]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 1
給contestant 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.