我有这个JavaScript文件(Rhino 1.7R4)。
importPackage(java.io);
importPackage(java.lang);
importPackage(java.util);
var reader = new BufferedReader( new InputStreamReader(System['in']) );
var line = reader.readLine();
var tok = new java.util.StringTokenizer(line);
var A = Integer.parseInt(tok.nextToken());
var B = Integer.parseInt(tok.nextToken());
var C = Integer.parseInt(tok.nextToken());
// System.out.printf( "A=%d, B=%d, C=%d\n", A, B, C );
System.out.printf( "A=%f, B=%f, C=%f\n", A, B, C );
当我第一次取消注释时printf
- 我正在
A=Exception in thread "main" org.mozilla.javascript.WrappedException: Wrapped java.util.IllegalFormatConversionException: d != java.lang.Double
at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1754)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:148)
at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
at org.mozilla.javascript.optimizer.OptRuntime.callN(OptRuntime.java:52)
at test._c_script_0(Unknown Source)
at test.call(Unknown Source)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
at test.call(Unknown Source)
at test.exec(Unknown Source)
at org.mozilla.javascript.optimizer.OptRuntime$1.run(OptRuntime.java:218)
at org.mozilla.javascript.Context.call(Context.java:489)
at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:504)
at org.mozilla.javascript.optimizer.OptRuntime.main(OptRuntime.java:206)
at test.main(Unknown Source)
Caused by: java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
... 13 more
答案 0 :(得分:3)
这是JavaScript类型系统的翻版。 JavaScript中的所有数字都是双精度数,因此`Integer.parseInt(" 4.5")返回4时,需要将其转换为4.0 for JavaScript。
幸运的是,64位双精度具有足够的精度来准确表示任何32位int,因此在JavaScript方面,您通常不必担心它。但是值后面的Java对象始终是Double
并在需要时自动转换。但由于System.out.printf
接受Object
类型的参数,因此必须按原样传递。