我刚接触Java,一直在读一本我的朋友借给我的书。然而,我认为这本书可能有点过时了。
我正在尝试打印价格,本书使用了下面的示例(packPrice和packVolume都是使用in.nextDouble()输入的双变量):
double pricePerOunce = packPrice / packVolume;
System.out.printf("Price per ounce: %8.2f", pricePerOunce);
System.out.println();但是,当我在Java SE 8u5 (Windows 64位)中尝试完全相同的代码时,我得到以下错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method printf(String, Object[]) in the type PrintStream is not applicable for
the arguments (String, double)有谁能解决我的问题吗?如果需要更多信息,请告诉我。
提前感谢!
发布于 2014-05-15 03:24:57
Why am I getting a compilation errors with a simple printf?
“检查您的项目的编译器合规性级别是否至少设置为1.5:
项目>属性> Java编译器
如果未设置启用特定于项目的设置,请使用配置工作区设置...链接,以检查全局编译器合规性级别。“
发布于 2014-05-15 03:30:06
输出具有小数精度的双精度值的另一种可能方法是这样完成的,
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));这个例子和类似的问题可以在这里找到:set double format with 2 decimal places
https://stackoverflow.com/questions/23663319
复制相似问题