在printf()或format()中添加新行。

huangapple go评论69阅读模式
英文:

Adding new line in printf() or format()

问题

今天我在Oracle的Java教程文档中阅读有关格式化数字打印输出的内容,我在理解了println()printf()format()之间的区别后,遇到了下面这句话,但对我来说毫无意义。

> 适用于运行应用程序的平台的换行符。您应该始终使用%n,而不是\n。

因此,这其中的主要问题是为什么我总是要使用%n而不是\n,这会有什么不同?

英文:

today I was reading in the java Tutorials docs from Oracle about Formatting Numeric Print Output and I encountered println() vs printf() vs format() after I understood the differences I read the following sentence which makes no sense for me.

> A new line character appropriate to the platform running the application. You should always use %n, rather than \n.

so the main question of this is why "should" I always use %n instead of \n what difference does this make?

答案1

得分: 0

%n 就像是一个针对特定系统换行符的占位符。对于某个特定系统,%n 可能是 \n,而对于另一个系统(比如 Windows),它可能是 \r\n。使用 %n 是比直接输入 \n 更安全的选择,因为正如 @user85421 所评论的,一些 Windows 应用程序无法识别 \n。

英文:

The %n is like a placeholder for whatever the newline character may be for a particular system. %n could very well be \n for a specific system or \r\n for another such as the case with windows. Using %n is the safer alternative to typing \n because as @user85421 commented, some windows applications don't recognize \n.

huangapple
  • 本文由 发表于 2020年4月3日 21:41:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/61013257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定