如何使用System.out.printf [英] How to use System.out.printf

查看:135
本文介绍了如何使用System.out.printf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 printf ,但我遇到了意外错误。

I tried to use printf but I have unexpected errors.

此代码的错误是什么:

What is the fault on this code:

System.out.printf("The date is %d/%d/%d", month,day,year);

我想打印日期和是双变量。

I want to print the date and month, day and year are double variables.

推荐答案

根据格式化程序文档%d 是一个积分值的转换,不适用于双打。你会想要将它们转换为整数。无论如何,为什么你将月,日和年表示为浮点数?你最好使用 日期 并使用相应的格式化程序代表日期值。

As per the Formatter docs, %d is a conversion for an integral value, which wouldn't work for doubles. You'll want to convert them to integers. Why would you represent month, day, and year as floating point numbers, anyway? You'd be much better off using a Date and using an appropriate formatter for date values.

如果必须使用双精度值,那么你需要%f 而不是%d

If you must use doubles for those values, you'll want %f instead of %d.

这篇关于如何使用System.out.printf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆