public class TestScoreClass {
public static final int NUM = 30;
public static void main(String[] args){
ScoreClass[] score = new ScoreClass[NUM];
enterScore(score);
sort(score);
printScore(score);
}
public static void enterScore(ScoreClass[] score){
for(int i = 0; i < score.length; i++){
score[i] = new ScoreClass(1000+i,(int)(Math.random()*100));
}
}
public static void printScore(ScoreClass[] score){
for(int i=0; i < score.length;i++){
System.out.printf("%4d",score[i]);
}
}
public static void sort(ScoreClass[] score){
int index;
for(int i = 0;i < score.length-1;i++){
index = i;
for(int j = i+1;j < score.length; j++){
if(score[j].getScore() > score[index].getScore()){
index = j;
}
if(index!=i){
ScoreClass s = score[i];
score[i] = score[index];
score[index] = s;
}
}
}
}
}
控制台:
Exception in thread "main" java.util.IllegalFormatConversionException: d != ScoreClass
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3992)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2708)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2660)
at java.util.Formatter.format(Formatter.java:2432)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at TestScoreClass.printScore(TestScoreClass.java:21)
at TestScoreClass.main(TestScoreClass.java:8)