Par*_*ore 1 java printing string variables println
我现在正在编写一些代码,但我已经有一段时间没有这样做了,我将如何使用 println 函数来编写字符串和变量?
我尝试了以下方法:
System.out.println("randomtext"var);
Run Code Online (Sandbox Code Playgroud)
和
System.out.println("randomtext",var);
Run Code Online (Sandbox Code Playgroud)
这些似乎都不起作用,我做错了什么?我该怎么做呢?
您要么需要使用 + 来连接,要么使用 String.format 来格式化字符串或类似内容。
最简单的是进行连接:
System.out.println("randomtext" + var);
Run Code Online (Sandbox Code Playgroud)
有些人经常使用System.out.printfwhich 的工作方式与String.format您想要连接的大杂乱字符串相同:
System.out.printf("This is %s lot of information about %d and %s", var, integer, stringVar);
Run Code Online (Sandbox Code Playgroud)
使用 String.format 时,您需要了解的最重要的格式化程序是%s字符串、%f浮点数和%d整数。
关于如何使用 String.format 的更多信息可以在关于 Formatter的文档中找到。