2017-01-15 17 views
0
System.out.printf("The Sum is %d%n" , sum); 

和誤差是 的方法的printf在類型爲PrintStream(字符串,對象[])是不適用的參數(字符串,整數)如何打印變量的值中的java

System.out.printf("The sum is " + sum); 

運作,但如果我需要打印

"The Sum of 5 and 6 is 11"

System.out.printf("The sum of %d and %d is %d . " ,a,b,sum); 

,但得到的Eclipse平臺版本上述錯誤:3.8.1(Ubuntu的)

+1

在'System.out.printf的錯誤( 「一加一%d%N」,總和);'是%n,你當然是\ n。 – Turo

+0

異常在線程「主要」 java.lang.Error的:未解決問題彙編: \t的方法的printf在類型爲PrintStream(字符串,對象[])是不適用的參數(字符串,整數) \t在加法。 main(addition.java:25) 是錯誤。 –

+0

@Turo'%n'是有效的。 – Tom

回答

3

如果System.out.printf是給你這個錯誤簡單的方法:

The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int) 

然後您必須配置爲正確的Java版本的項目。

可變參數的方法與Java 5

或者進行了介紹,你可以這樣做:

System.out.printf("The Sum of %d and %d is %d\n", new Object[] {a, b, sum}); 
2
int a = 2; 
int b = 4; 
int c = a + b; 
System.out.format("The sum of %d and %d is %d ", a, b, c); 

這是格式化輸出