在布尔值上使用printf

时间:2013-12-14 21:00:08

标签: java arraylist printf

我有一个关于如何使用printf显示布尔值的问题。就我而言,我认为唯一的问题是显示带有printf的布尔类型。我怎样才能解决这个问题?任何帮助将不胜感激。当我运行我的程序时,我得到了这个:

run:
_____________________________________________________________________________________________________________
                   |        Household Waste Recycled        |     |            Pounds of CO2            |
_____________________________________________________________________________________________________________
Index      People       Paper     Plastic       Glass        Cans      Total Emission   ReductionNet Emission

Exception in thread "main" java.util.IllegalFormatConversionException: f != CO2FromWaste
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4045)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2761)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2708)
at java.util.Formatter.format(Formatter.java:2488)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at CO2FromWasteTester.main(CO2FromWasteTester.java:55)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

以下是我的主要方法文件的代码:

import java.util.ArrayList;

public class CO2FromWasteTester {

public static void main(String args[]){

    ArrayList<CO2FromWaste> CO2 = new ArrayList<CO2FromWaste>();

    CO2.add(new CO2FromWaste(1, true, true, true, true));
    CO2.add(new CO2FromWaste(2, true, false, true, false));
    CO2.add(new CO2FromWaste(3, true, true, true, false));
    CO2.add(new CO2FromWaste(4, false, false, false, true));
    CO2.add(new CO2FromWaste(5, false, true, false, true));
    CO2.add(new CO2FromWaste(6, true, true, true, true));

    CO2FromWaste data;

    for(int i = 0; i < CO2.size(); i++){
        data = CO2.get(i);
        data.getNumberOfPeople();
        data.Paper();
        data.Plastic();
        data.Glass();
        data.Can();
        data.NetEmission();
        data.reduction();
        data.totalEmission();
    }

    // create table headings
    System.out.println("___________________________________________________"
            + "__________________________________________________________");
    System.out.printf("%65s%44s%n", "|        Household Waste Recycled        |",
            "|            Pounds of CO2            |");
    System.out.println("___________________________________________________"
            + "__________________________________________________________");
    System.out.printf("%1s%12s%12s%12s%12s%12s%20s%12s%12s%n%n", "Index", "People", 
            "Paper", "Plastic", "Glass", "Cans", "Total Emission", "Reduction", 
            "Net Emission");

    // call methods
    for(int i = 0; i < CO2.size(); i++){
        data = CO2.get(i);
        System.out.printf("%1f%1f%5b%5b%5b%5b%4.2f%4.2f%4.2f", 
                data = CO2.get(i), 
                data.getNumberOfPeople(), 
                data.Paper(), 
                data.Plastic(), 
                data.Glass(), 
                data.Can(), 
                data.totalEmission(), 
                data.reduction(), 
                data.NetEmission());
    }
}

}

2 个答案:

答案 0 :(得分:1)

这是因为您正在重复通话data = CO2.get(i),而且无法打印。试试这个:

for(int i = 0; i < CO2.size(); i++){
        data = CO2.get(i);
        System.out.printf("%1f%1f%5b%5b%5b%5b%4.2f%4.2f%4.2f", 
                data.getNumberOfPeople(), 
                data.Paper(), 
                data.Plastic(), 
                data.Glass(), 
                data.Can(), 
                data.totalEmission(), 
                data.reduction(), 
                data.NetEmission());
}

答案 1 :(得分:0)

考虑使用三元运算符:

printf(&#34;%s&#34;,(true == value)?&#34; true&#34;:&#34; false&#34;);