如何在java中打印变量的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41660150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print the value of variable in java
提问by Sagar Devkota
System.out.printf("The Sum is %d%n" , sum);
and the error is The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int)
并且错误是 PrintStream 类型中的方法 printf(String, Object[]) 不适用于参数 (String, int)
System.out.printf("The sum is " + sum);
Works but What if i need to print
有效,但如果我需要打印怎么办
"The Sum of 5 and 6 is 11"
“5 和 6 的和是 11”
System.out.printf("The sum of %d and %d is %d . " ,a,b,sum);
But got the above error on Eclipse Platform Version: 3.8.1 (Ubuntu)
但在 Eclipse 平台版本上出现上述错误:3.8.1 (Ubuntu)
采纳答案by john16384
If System.out.printf
is giving you this error:
如果System.out.printf
给你这个错误:
The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int)
Then you must configure your project for the proper Java version.
然后您必须为正确的 Java 版本配置您的项目。
Methods with variable arguments were introduced with Java 5.
带有可变参数的方法是在 Java 5 中引入的。
Alternatively, you could do:
或者,你可以这样做:
System.out.printf("The Sum of %d and %d is %d\n", new Object[] {a, b, sum});
回答by ppasler
Have look at this question for your error: Eclipse Java printf issue PrintStream is not applicable
看看这个问题是否有你的错误:Eclipse Java printf 问题 PrintStream 不适用
Alternatively you can use .format
或者你可以使用 .format
System.out.format("The sum of %d and %d is %d . " ,1, 2, 3);
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
With proper Java version set .printf
also works as expected
使用正确的 Java 版本设置.printf
也可以按预期工作
System.out.printf("The sum of %d and %d is %d . " ,1, 2, 3);
回答by Punit Inani
int a = 2;
int b = 4;
int c = a + b;
System.out.format("The sum of %d and %d is %d ", a, b, c);
This is simple way in the formatted output
这是格式化输出中的简单方法