java.util.UnknownFormatConversionException:转化率='N'

java.util.UnknownFormatConversionException: Conversion = 'N'

这是我的代码:

package com.example.java;

public class PackageDataTest {

    public static void main(String[] args) {
        PackageData data1 = new PackageData("John Davids");
        System.out.printf("%Number of object created until now is : %d", PackageData.count);
    }
}

class PackageData {

    static int count = 0;
    String name;

    public PackageData(){
        this.name = "";
        ++count;
    }

    public PackageData(String name) {
        this.name = name;
        ++count;
    }

    public int getCount() { return count; }
}

我不断收到以下错误:

"Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'N'"

%N 不是有效的转换格式,%n 用于换行。

因此,如果您按以下方式替换代码:

System.out.printf("%nNumber of object created until now is : %d", PackageData.count); 

输出将换行:

Number of object created until now is : 1

查看此讨论:What's up with Java's "%n" in printf?