英尺和米之间的转换

时间:2014-11-30 21:59:34

标签: java

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("This program converts between feet and meters");
    System.out.printf("%5s%12s%6s%12s%12s\n","Feet","Meters","|","Meters","Feet");
    System.out.println("                                                    ");

    for(double f = 1.0, m = 20; f <= 10.0; f++, m += 5.0){
        System.out.printf("%5.1f%12.3f%6s%12,1f%12.3f\n", f, footToMeter(f), "|", m, meterToFoot(m));
    }

}
public static double footToMeter(double foot){
    return foot * 0.3048;
}
public static double meterToFoot(double meter){
    return meter / 0.3048;
}

结果应该是:

Feet Meters | Meters Feet
1.0  0.305  | 20.0   65.574
2.0  0.610  | 25.0   81.967

但我得到以下例外:

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '1'
at java.util.Formatter.checkText(Formatter.java:2503)
at java.util.Formatter.parse(Formatter.java:2467)
at java.util.Formatter.format(Formatter.java:2414)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at Ex6_9.main(Ex6_9.java:15)

1 个答案:

答案 0 :(得分:0)

这行中有拼写错误

System.out.printf("%5.1f%12.3f%6s%12,1f%12.3f\n", f, footToMeter(f), "|", m, meterToFoot(m));

具体而言,"%5.1f%12.3f%6s%12,1f%12.3f\n"需要更改为"%5.1f%12.3f%6s%12.1f%12.3f\n"。请注意,逗号已更改为句点。