SCJP - 数字格式

jyo*_*jyo 0 java scjp certificate number-formatting

鉴于:

public class LineUp {
    public static void main(String[] args) {
        double d = 12.345;
        // insert code here
    }
}
Run Code Online (Sandbox Code Playgroud)

在第4行插入哪个代码片段会产生输出| 12.345|

A. System.out.printf("|%7d| \n", d);
B. System.out.printf("|%7f| \n", d);
C. System.out.printf("|%3.7d| \n", d);
D. System.out.printf("|%3.7f| \n", d);
E. System.out.printf("|%7.3d| \n", d);
F. System.out.printf("|%7.3f| \n", d);
Answer: F
Run Code Online (Sandbox Code Playgroud)

printf语句的解释是什么,为什么是|%7d | 正在给予illegalFormatConversionException ?

谢谢

Rad*_*scu 5

因为d是double并且它不能被格式化为十进制整数.在浮点变量的情况下你不能使用"d"格式描述符,没有显式强制转换以表明你知道的事实可能的精度损失.