英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论