如何在java中打印变量的值

新手上路,请多包涵
System.out.printf("The Sum is %d%n" , sum);

错误是 PrintStream 类型中的方法 printf(String, Object[]) 不适用于参数 (String, int)

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

有效,但如果我需要打印怎么办

“5和6之和是11”

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

但在 Eclipse Platform Version: 3.8.1 (Ubuntu) 上出现上述错误

原文由 Sagar Devkota 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 994
2 个回答

如果 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});

原文由 john16384 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏