Java: System.out.format IllegalFormatPrecision 异常
Java: System.out.format IllegalFormatPrecision Exception
考虑以下打印图表的循环:
假设数组中的所有元素以及 interest 都有一个赋值,为什么 for 循环(确切地说是 System.out.format)会产生 IllegalFormatPrecisionException?
double[] sbalance;
double[] fbalance;
double[] interest;
sbalance = new double[months];
fbalance = new double[months];
interest = new double[months];
int deposit;
for (int q = 1; q <= months; q++)
{
System.out.format ("%18.2d%", sbalance[q-1]);
System.out.format ("%18.2d%", interest [q-1]);
System.out.format ("%18.2d%", (double)deposit);
System.out.format ("%18.2d%", fbalance [q-1]);
}
即使我返回并将我的数组重新声明为 Double[] 而不是 double[],程序也会产生相同的错误(我意识到 java 与自动装箱不一致)
非常感谢任何帮助。
使用 %f
而不是 %d
。
前者格式化浮点数;后者格式化整数。要求打印一个小数点后两位的整数是没有意义的。
格式中的d
表示十进制整数,不是双精度数。你应该使用 f
.
有两个问题。
- 在格式字符串中,
%18.2d
应该是 %18.2f
。
- 在格式字符串的末尾,
%
应该是 %n
。 (在 cron 文件中,%
表示换行,但在 Java 中,%n
表示换行。)
这是工作代码:
public class Interest {
public static void main(String[] args) {
int months = 12;
int deposit = 1000;
double[] sbalance;
double[] fbalance;
double[] interest;
sbalance = new double[months];
fbalance = new double[months];
interest = new double[months];
for (int q = 1; q <= months; q++) {
System.out.format ("Month %d%n", q);
System.out.format ("%18.2f%n", sbalance[q-1]);
System.out.format ("%18.2f%n", interest [q-1]);
System.out.format ("%18.2f%n", (double)deposit);
System.out.format ("%18.2f%n", fbalance [q-1]);
}
}
}
如果您想在编译时收到 IllegalFormatPrecision 错误的警告,而不是遭受 运行 时的崩溃,您可以使用 Format String Checker of the Checker Framework。如果您安装了它并且 运行
$CHECKERFRAMEWORK/checker/bin/javac -processor formatter Interest.java
然后编译器会通过特定的错误消息警告您问题。
考虑以下打印图表的循环: 假设数组中的所有元素以及 interest 都有一个赋值,为什么 for 循环(确切地说是 System.out.format)会产生 IllegalFormatPrecisionException?
double[] sbalance;
double[] fbalance;
double[] interest;
sbalance = new double[months];
fbalance = new double[months];
interest = new double[months];
int deposit;
for (int q = 1; q <= months; q++)
{
System.out.format ("%18.2d%", sbalance[q-1]);
System.out.format ("%18.2d%", interest [q-1]);
System.out.format ("%18.2d%", (double)deposit);
System.out.format ("%18.2d%", fbalance [q-1]);
}
即使我返回并将我的数组重新声明为 Double[] 而不是 double[],程序也会产生相同的错误(我意识到 java 与自动装箱不一致)
非常感谢任何帮助。
使用 %f
而不是 %d
。
前者格式化浮点数;后者格式化整数。要求打印一个小数点后两位的整数是没有意义的。
格式中的d
表示十进制整数,不是双精度数。你应该使用 f
.
有两个问题。
- 在格式字符串中,
%18.2d
应该是%18.2f
。 - 在格式字符串的末尾,
%
应该是%n
。 (在 cron 文件中,%
表示换行,但在 Java 中,%n
表示换行。)
这是工作代码:
public class Interest {
public static void main(String[] args) {
int months = 12;
int deposit = 1000;
double[] sbalance;
double[] fbalance;
double[] interest;
sbalance = new double[months];
fbalance = new double[months];
interest = new double[months];
for (int q = 1; q <= months; q++) {
System.out.format ("Month %d%n", q);
System.out.format ("%18.2f%n", sbalance[q-1]);
System.out.format ("%18.2f%n", interest [q-1]);
System.out.format ("%18.2f%n", (double)deposit);
System.out.format ("%18.2f%n", fbalance [q-1]);
}
}
}
如果您想在编译时收到 IllegalFormatPrecision 错误的警告,而不是遭受 运行 时的崩溃,您可以使用 Format String Checker of the Checker Framework。如果您安装了它并且 运行
$CHECKERFRAMEWORK/checker/bin/javac -processor formatter Interest.java
然后编译器会通过特定的错误消息警告您问题。