javaoop使用代码将int[] scores={16,25,9,90,23}; 数组由大到小进行排列打印,并给出对应的排名号
时间: 2023-05-19 11:07:25 浏览: 94
以下是使用Java OOP实现的代码:
```
public class ScoreRanking {
private int[] scores;
public ScoreRanking(int[] scores) {
this.scores = scores;
}
public void printRanking() {
int[] sortedScores = Arrays.copyOf(scores, scores.length);
Arrays.sort(sortedScores);
for (int i = sortedScores.length - 1; i >= 0; i--) {
int score = sortedScores[i];
int rank = getRank(score);
System.out.println("Score: " + score + ", Rank: " + rank);
}
}
private int getRank(int score) {
int rank = 1;
for (int s : scores) {
if (s > score) {
rank++;
}
}
return rank;
}
}
// Usage:
int[] scores = {16, 25, 9, 90, 23};
ScoreRanking ranking = new ScoreRanking(scores);
ranking.printRanking();
```
输出结果为:
```
Score: 90, Rank: 1
Score: 25, Rank: 2
Score: 23, Rank: 3
Score: 16, Rank: 4
Score: 9, Rank: 5
```
其中,排名号越小表示成绩越好。
关于中文加密的问题,我不太明白您具体想问什么,可以再提供一些信息吗?
阅读全文