java se IllegalFormateConverssionException

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)

0

2个回答

System.out.printf("%4d",score[i]);
这是Java,不是C,什么%4d

1
sinat_34458693
sinat_34458693 谢谢回答~
大约 3 年之前 回复

语法错误,格式化混淆啊

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!