2 sinat 34458693 sinat_34458693 于 2016.04.03 22:08 提问

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)

2个回答

caozhy
caozhy   Ds   Rxr 2016.04.03 22:15
已采纳

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

sinat_34458693
sinat_34458693 谢谢回答~
一年多之前 回复
guanjun_12
guanjun_12   2017.12.10 11:09

语法错误,格式化混淆啊

Csdn user default icon
上传中...
上传图片
插入图片