gpt4 book ai didi

java - 对 boolean 值使用 printf

转载 作者:行者123 更新时间:2023-12-01 13:42:39 28 4
gpt4 key购买 nike

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

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());
}
}

}

最佳答案

这是因为您重复调用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());
}

关于java - 对 boolean 值使用 printf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20588201/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com