英文:
Ranking array Java
问题
以下是翻译好的部分:
我正在尝试对我的作业中的数组进行排名,但我不理解如何做。有人可以帮帮我吗?提前谢谢您。
我附上了完整作业说明的图片。
这是我的作业图片:
[![enter image description here][1]][1]
这是我的代码:
public class Assignment_3_Question_1 {
// 这里,我预先定义了我的数据集。
static int referenceScore[][]={{39,40,17,35,42,6},{40,41,27,41,42,36},{42,40,26,42,42,35}};
static int finalScore[][]={{39,40,17,35,42,6},{40,41,27,41,42,36},{42,40,26,42,42,35}};
static int scoreAmongOthers[][]=new int[3][6];
static int max;
static int rank = 1;
static int count = 0;
static int total = 0;
public static void main(String[] args) {
for (int k = 0; k < 10; k++){
max = referenceScore[0][0];
for (int team = 0; team < 3; team++){
for (int position = 0; position < 6; position++){
if (max < referenceScore[team][position]){
max = referenceScore[team][position];
}
}
}
for (int x = 0; x < 3; x++){
for(int y = 0; y < 6; y++){
if(referenceScore[x][y]==max){
scoreAmongOthers[x][y]=rank;
referenceScore[x][y]=0;
count++;
}
}
}
rank = count + 1;
}
// 打印出
System.out.println("\tP1\tP2\tP3\tP4\tP5\tP6\tTotal\tRank");
// 打印出每个团队的结果和排名
for(int teamNb = 0; teamNb<3; teamNb++){
System.out.print("Team"+(teamNb+1));
for(int p=0; p<6; p++){
total = total + finalScore[teamNb];
System.out.print("\t" + finalScore[teamNb]
+"("+ scoreAmongOthers[teamNb]
+") ");
}
System.out.print("\t"+ total);
total = 0;
System.out.println();
}
}
}
[1]: https://i.stack.imgur.com/6tRhj.png
英文:
I am trying to rank my array in my assignment but I don't understand how to do it. Can someone help me? Thank you in advance
I added an image of the full assignment instructions.
Here is an image of my assignment:
And here is my code:
public class Assignment_3_Question_1 {
// Here, I predefined my data set.
static int referenceScore[][]={{39,40,17,35,42,6},{40,41,27,41,42,36},{42,40,26,42,42,35}};
static int finalScore[][]={{39,40,17,35,42,6},{40,41,27,41,42,36},{42,40,26,42,42,35}};
static int scoreAmongOthers[][]=new int[3][6];
static int max;
static int rank = 1;
static int count = 0;
static int total = 0;
public static void main(String[] args) {
for (int k = 0; k < 10; k++){
max = referenceScore[0][0];
for (int team = 0; team < 3; team++){
for (int position = 0; position < 6; position++){
if (max < referenceScore[team][position]){
max = referenceScore[team][position];
}
}
}
for (int x = 0; x < 3; x++){
for(int y = 0; y < 6; y++){
if(referenceScore[x][y]==max){
scoreAmongOthers[x][y]=rank;
referenceScore[x][y]=0;
count++;
}
}
}
rank = count + 1;
}
// Print out the
System.out.println("\tP1\tP2\tP3\tP4\tP5\tP6\tTotal\tRank");
// Prints out the results and the rank for each team
for(int teamNb = 0; teamNb<3; teamNb++){
System.out.print("Team"+(teamNb+1));
for(int p=0; p<6; p++){
total = total + finalScore[teamNb];
System.out.print("\t" + finalScore[teamNb]
+"("+ scoreAmongOthers[teamNb]
+") ");
}
System.out.print("\t"+ total);
total = 0;
System.out.println();
}
}
}
答案1
得分: 2
以下是翻译好的内容:
public class RankScores {
private static int getRank(int[][] scores, int rowIndex, int columnIndex) {
int count = 0;
int score = scores[rowIndex][columnIndex];
for (int row = 0; row < scores.length; row++) {
for (int col = 0; col < scores[row].length; col++) {
if (scores[row][col] > score) {
count++;
}
}
}
return count + 1;
}
private static int getTeamRank(int[] scores, int index) {
int count = 0;
int score = scores[index];
for (int i = 0; i < scores.length; i++) {
if (scores[i] > score) {
count++;
}
}
return count + 1;
}
public static void main(String[] args) {
int referenceScore[][]={{39,40,17,35,42,6},{40,41,27,41,42,36},{42,40,26,42,42,35}};
int ranks[][] = new int[referenceScore.length][];
int totals[] = new int[referenceScore.length]; // total score for each team
int teamRanks[] = new int[referenceScore.length];
for (int row = 0; row < ranks.length; row++) {
ranks[row] = new int[referenceScore[row].length];
totals[row] = 0;
for (int col = 0; col < ranks[row].length; col++) {
ranks[row][col] = getRank(referenceScore, row, col);
totals[row] += referenceScore[row][col];
}
}
for (int team = 0; team < teamRanks.length; team++) {
teamRanks[team] = getTeamRank(totals, team);
}
System.out.println(" P1 P2 P3 P4 P5 P6 Total Rank");
for (int row = 0; row < ranks.length; row++) {
System.out.print("Team " + (row + 1) + " ");
for (int col = 0; col < ranks[row].length; col++) {
System.out.printf("%2d(%2d) ", referenceScore[row][col], ranks[row][col]);
}
System.out.println(" " + totals[row] + " " + teamRanks[row]);
}
}
}
上述代码运行后产生以下输出:
P1 P2 P3 P4 P5 P6 Total Rank
Team 1 39(11) 40( 8) 17(17) 35(13) 42( 1) 6(18) 179 3
Team 2 40( 8) 41( 6) 27(15) 41( 6) 42( 1) 36(12) 227 1
Team 3 42( 1) 40( 8) 26(16) 42( 1) 42( 1) 35(13) 227 1
英文:
So I understand that the point of the exercise is to practice working with arrays. You have outlined the required algorithm in your comment. Here is my implementation.
public class RankScores {
private static int getRank(int[][] scores, int rowIndex, int columnIndex) {
int count = 0;
int score = scores[rowIndex][columnIndex];
for (int row = 0; row < scores.length; row++) {
for (int col = 0; col < scores[row].length; col++) {
if (scores[row][col] > score) {
count++;
}
}
}
return count + 1;
}
private static int getTeamRank(int[] scores, int index) {
int count = 0;
int score = scores[index];
for (int i = 0; i < scores.length; i++) {
if (scores[i] > score) {
count++;
}
}
return count + 1;
}
public static void main(String[] args) {
int referenceScore[][]={{39,40,17,35,42,6},{40,41,27,41,42,36},{42,40,26,42,42,35}};
int ranks[][] = new int[referenceScore.length][];
int totals[] = new int[referenceScore.length]; // total score for each team
int teamRanks[] = new int[referenceScore.length];
for (int row = 0; row < ranks.length; row++) {
ranks[row] = new int[referenceScore[row].length];
totals[row] = 0;
for (int col = 0; col < ranks[row].length; col++) {
ranks[row][col] = getRank(referenceScore, row, col);
totals[row] += referenceScore[row][col];
}
}
for (int team = 0; team < teamRanks.length; team++) {
teamRanks[team] = getTeamRank(totals, team);
}
System.out.println(" P1 P2 P3 P4 P5 P6 Total Rank");
for (int row = 0; row < ranks.length; row++) {
System.out.print("Team " + (row + 1) + " ");
for (int col = 0; col < ranks[row].length; col++) {
System.out.printf("%2d(%2d) ", referenceScore[row][col], ranks[row][col]);
}
System.out.println(" " + totals[row] + " " + teamRanks[row]);
}
}
}
Running the above code produces the following output.
P1 P2 P3 P4 P5 P6 Total Rank
Team 1 39(11) 40( 8) 17(17) 35(13) 42( 1) 6(18) 179 3
Team 2 40( 8) 41( 6) 27(15) 41( 6) 42( 1) 36(12) 227 1
Team 3 42( 1) 40( 8) 26(16) 42( 1) 42( 1) 35(13) 227 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论