java - Java中使用printf显示信息

标签 java printf

我有一个关于如何使用 printf 进行格式化的问题。我使用了 printf 3 次;这 3 次中,我每次都使用完全相同的 printf,使用 1s%10.2f%n1。不知何故,我认为我第三次使用 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;
}

}

最佳答案

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

关于java - Java中使用printf显示信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20457307/

相关文章:

java - 为什么存在WeakHashMap,却没有WeakSet?

java - protected 类成员可以像 Java 中的公共(public)类成员一样被访问

java - 让我的 GUI 从类创建实例并将该信息拉入文本区域

java - 删除 POST 主体 Retrofit 中的可选参数

java - 按特定数字对列表进行排序?

java - 什么时候应该开始使用字符串替换而不是 sprintf?

c - printf 语句未在 netbean 中的 scanf 语句之前执行

c - 打印格式

c - 如何将 asprintf 与 Boehm GC 一起使用?

c++ - 使用 sprintf - 首先查询大小或传递 MAX_PATH 大数组