排名数组 Java

huangapple 未分类评论66阅读模式
英文:

Ranking array Java

问题

以下是翻译好的部分:

  1. 我正在尝试对我的作业中的数组进行排名,但我不理解如何做。有人可以帮帮我吗?提前谢谢您。
  2. 我附上了完整作业说明的图片。
  3. 这是我的作业图片:
  4. [![enter image description here][1]][1]
  5. 这是我的代码:

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");

  1. // 打印出每个团队的结果和排名
  2. for(int teamNb = 0; teamNb<3; teamNb++){
  3. System.out.print("Team"+(teamNb+1));
  4. for(int p=0; p<6; p++){
  5. total = total + finalScore[teamNb]

    ;

  6. System.out.print("\t" + finalScore[teamNb]

    +"("+ scoreAmongOthers[teamNb]

    +") ");

  7. }

  8. System.out.print("\t"+ total);

  9. total = 0;

  10. System.out.println();

  11. }

  12. }

}

  1. [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:

排名数组 Java

And here is my code:

  1. public class Assignment_3_Question_1 {
  2. // Here, I predefined my data set.
  3. static int referenceScore[][]={{39,40,17,35,42,6},{40,41,27,41,42,36},{42,40,26,42,42,35}};
  4. static int finalScore[][]={{39,40,17,35,42,6},{40,41,27,41,42,36},{42,40,26,42,42,35}};
  5. static int scoreAmongOthers[][]=new int[3][6];
  6. static int max;
  7. static int rank = 1;
  8. static int count = 0;
  9. static int total = 0;
  10. public static void main(String[] args) {
  11. for (int k = 0; k &lt; 10; k++){
  12. max = referenceScore[0][0];
  13. for (int team = 0; team &lt; 3; team++){
  14. for (int position = 0; position &lt; 6; position++){
  15. if (max &lt; referenceScore[team][position]){
  16. max = referenceScore[team][position];
  17. }
  18. }
  19. }
  20. for (int x = 0; x &lt; 3; x++){
  21. for(int y = 0; y &lt; 6; y++){
  22. if(referenceScore[x][y]==max){
  23. scoreAmongOthers[x][y]=rank;
  24. referenceScore[x][y]=0;
  25. count++;
  26. }
  27. }
  28. }
  29. rank = count + 1;
  30. }
  31. // Print out the
  32. System.out.println(&quot;\tP1\tP2\tP3\tP4\tP5\tP6\tTotal\tRank&quot;);
  33. // Prints out the results and the rank for each team
  34. for(int teamNb = 0; teamNb&lt;3; teamNb++){
  35. System.out.print(&quot;Team&quot;+(teamNb+1));
  36. for(int p=0; p&lt;6; p++){
  37. total = total + finalScore[teamNb]

    ;

  38. System.out.print(&quot;\t&quot; + finalScore[teamNb]

    +&quot;(&quot;+ scoreAmongOthers[teamNb]

    +&quot;) &quot;);

  39. }

  40. System.out.print(&quot;\t&quot;+ total);

  41. total = 0;

  42. System.out.println();

  43. }

  44. }

  45. }

答案1

得分: 2

以下是翻译好的内容:

  1. public class RankScores {
  2. private static int getRank(int[][] scores, int rowIndex, int columnIndex) {
  3. int count = 0;
  4. int score = scores[rowIndex][columnIndex];
  5. for (int row = 0; row < scores.length; row++) {
  6. for (int col = 0; col < scores[row].length; col++) {
  7. if (scores[row][col] > score) {
  8. count++;
  9. }
  10. }
  11. }
  12. return count + 1;
  13. }
  14. private static int getTeamRank(int[] scores, int index) {
  15. int count = 0;
  16. int score = scores[index];
  17. for (int i = 0; i < scores.length; i++) {
  18. if (scores[i] > score) {
  19. count++;
  20. }
  21. }
  22. return count + 1;
  23. }
  24. public static void main(String[] args) {
  25. int referenceScore[][]={{39,40,17,35,42,6},{40,41,27,41,42,36},{42,40,26,42,42,35}};
  26. int ranks[][] = new int[referenceScore.length][];
  27. int totals[] = new int[referenceScore.length]; // total score for each team
  28. int teamRanks[] = new int[referenceScore.length];
  29. for (int row = 0; row < ranks.length; row++) {
  30. ranks[row] = new int[referenceScore[row].length];
  31. totals[row] = 0;
  32. for (int col = 0; col < ranks[row].length; col++) {
  33. ranks[row][col] = getRank(referenceScore, row, col);
  34. totals[row] += referenceScore[row][col];
  35. }
  36. }
  37. for (int team = 0; team < teamRanks.length; team++) {
  38. teamRanks[team] = getTeamRank(totals, team);
  39. }
  40. System.out.println(" P1 P2 P3 P4 P5 P6 Total Rank");
  41. for (int row = 0; row < ranks.length; row++) {
  42. System.out.print("Team " + (row + 1) + " ");
  43. for (int col = 0; col < ranks[row].length; col++) {
  44. System.out.printf("%2d(%2d) ", referenceScore[row][col], ranks[row][col]);
  45. }
  46. System.out.println(" " + totals[row] + " " + teamRanks[row]);
  47. }
  48. }
  49. }

上述代码运行后产生以下输出:

  1. P1 P2 P3 P4 P5 P6 Total Rank
  2. Team 1 39(11) 40( 8) 17(17) 35(13) 42( 1) 6(18) 179 3
  3. Team 2 40( 8) 41( 6) 27(15) 41( 6) 42( 1) 36(12) 227 1
  4. 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.

  1. public class RankScores {
  2. private static int getRank(int[][] scores, int rowIndex, int columnIndex) {
  3. int count = 0;
  4. int score = scores[rowIndex][columnIndex];
  5. for (int row = 0; row &lt; scores.length; row++) {
  6. for (int col = 0; col &lt; scores[row].length; col++) {
  7. if (scores[row][col] &gt; score) {
  8. count++;
  9. }
  10. }
  11. }
  12. return count + 1;
  13. }
  14. private static int getTeamRank(int[] scores, int index) {
  15. int count = 0;
  16. int score = scores[index];
  17. for (int i = 0; i &lt; scores.length; i++) {
  18. if (scores[i] &gt; score) {
  19. count++;
  20. }
  21. }
  22. return count + 1;
  23. }
  24. public static void main(String[] args) {
  25. int referenceScore[][]={{39,40,17,35,42,6},{40,41,27,41,42,36},{42,40,26,42,42,35}};
  26. int ranks[][] = new int[referenceScore.length][];
  27. int totals[] = new int[referenceScore.length]; // total score for each team
  28. int teamRanks[] = new int[referenceScore.length];
  29. for (int row = 0; row &lt; ranks.length; row++) {
  30. ranks[row] = new int[referenceScore[row].length];
  31. totals[row] = 0;
  32. for (int col = 0; col &lt; ranks[row].length; col++) {
  33. ranks[row][col] = getRank(referenceScore, row, col);
  34. totals[row] += referenceScore[row][col];
  35. }
  36. }
  37. for (int team = 0; team &lt; teamRanks.length; team++) {
  38. teamRanks[team] = getTeamRank(totals, team);
  39. }
  40. System.out.println(&quot; P1 P2 P3 P4 P5 P6 Total Rank&quot;);
  41. for (int row = 0; row &lt; ranks.length; row++) {
  42. System.out.print(&quot;Team &quot; + (row + 1) + &quot; &quot;);
  43. for (int col = 0; col &lt; ranks[row].length; col++) {
  44. System.out.printf(&quot;%2d(%2d) &quot;, referenceScore[row][col], ranks[row][col]);
  45. }
  46. System.out.println(&quot; &quot; + totals[row] + &quot; &quot; + teamRanks[row]);
  47. }
  48. }
  49. }

Running the above code produces the following output.

  1. P1 P2 P3 P4 P5 P6 Total Rank
  2. Team 1 39(11) 40( 8) 17(17) 35(13) 42( 1) 6(18) 179 3
  3. Team 2 40( 8) 41( 6) 27(15) 41( 6) 42( 1) 36(12) 227 1
  4. Team 3 42( 1) 40( 8) 26(16) 42( 1) 42( 1) 35(13) 227 1

huangapple
  • 本文由 发表于 2020年3月16日 06:55:53
  • 转载请务必保留本文链接:https://java.coder-hub.com/60698495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定