在Java中使用printf显示信息

时间:2013-12-08 18:36:49

标签: java printf

我有一个关于如何使用printf格式化的问题。我使用printf 3次; 3次中的每一次,我都使用1s%10.2f%n1完全相同的printf。不知何故,我不认为我的第三次使用printf是有效的。我怎样才能解决这个问题?任何帮助将不胜感激。当我编译并执行它时,我得到了这个:

run:
Average Monthly Electricity Bill:     463.26
Average Monthly Electricity Price Per Kilowatt:       4.83
Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '10.2f'
at java.util.Formatter.format(Formatter.java:2487)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at CO2FromElectricityTester.main(CO2FromElectricityTester.java:43)
CO2 Emissions from Electricity Usage in a 3 Month Period: 394.56000000000006Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

以下是我写的两个文件:

CO2FromElectricityTester.java:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author timothylee
 */
public class CO2FromElectricityTester {

public static void main(String args[]){

    // declare & initialize variables
    double months = 3.0;
    double emissionFactor = 1.37;
    int kilowattHoursSept = 109;
    int kilowattHoursOct = 87;
    int kilowattHoursNov = 93;
    double monthlyCostSept = 551.51;
    double monthlyCostOct = 392.84;
    double monthlyCostNov = 445.42;
    double avgKilowattHours = (kilowattHoursSept + kilowattHoursOct + 
            kilowattHoursNov) / 3;
    double avgMonthlyCost = (monthlyCostSept + monthlyCostOct + 
            monthlyCostNov) / 3;

    // create object
    CO2FromElectricity CO2 = new CO2FromElectricity();

    // declare & initialize variables for methods
    double avgPricePerKilowatt = CO2.calcPricePerKilowatt(avgKilowattHours, 
            avgMonthlyCost);
    double avgCO2Emission = CO2.calcCO2Emission(emissionFactor, months, 
            avgMonthlyCost, avgPricePerKilowatt);

    ///////////////// display results
    System.out.printf("%1s%10.2f%n", "Average Monthly Electricity Bill: ", 
            avgMonthlyCost);
    System.out.printf("%1s%10.2f%n", "Average Monthly Electricity Price Per "
            + "Kilowatt: ", avgPricePerKilowatt);
    System.out.printf("%1s%10.2f%n", "CO2 Emissions from Electricity Usage "
            + "in a 3 Month Period: " + avgCO2Emission);
}

}

CO2FromElectricity.java:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author timothylee
 */
public class CO2FromElectricity {

// default constructor
CO2FromElectricity(){

}

// method for calculating price per kilowatt
public double calcPricePerKilowatt(double kilowattHours, double monthlyCost){
    return monthlyCost / kilowattHours;
}

// method for calculating CO2 emission
public double calcCO2Emission(double emissionFactor, double months, 
        double avgMonthlyCost, double avgPricePerKilowatt){
    return (avgMonthlyCost / avgPricePerKilowatt) * emissionFactor * months;
}

}

1 个答案:

答案 0 :(得分:1)

前两个printf行,您在非格式化字符串和数字之间使用逗号。不工作的那个有+而不是逗号。