Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串格式——将两个单独的语句组合成一行_Java_String Formatting - Fatal编程技术网

Java 字符串格式——将两个单独的语句组合成一行

Java 字符串格式——将两个单独的语句组合成一行,java,string-formatting,Java,String Formatting,现在while循环中的行“你将被收取10美元的费用”和“你想关闭你的帐户吗?”分别出现在不同的行中,如 “将向您收取10美元的费用。” “您需要说明吗?” 如何更改代码的格式,使两条语句都打印在一行上。程序应该仍然可以正常工作。答案应显示为 “您将被收取10美元的费用。是否要关闭您的帐户?” 在适当的情况下,使用print而不是println来抑制换行符。将println()更改为print()。println()每次都从新行开始。您可以使用类似于print的功能。 例如,System.out.

现在while循环中的行“你将被收取10美元的费用”和“你想关闭你的帐户吗?”分别出现在不同的行中,如

“将向您收取10美元的费用。”
“您需要说明吗?”

如何更改代码的格式,使两条语句都打印在一行上。程序应该仍然可以正常工作。答案应显示为

“您将被收取10美元的费用。是否要关闭您的帐户?”


在适当的情况下,使用
print
而不是
println
来抑制换行符。

将println()更改为print()。println()每次都从新行开始。

您可以使用类似于print的功能。 例如,System.out.print在控制台的同一行中显示输出。 System.out.println以单独的行显示输出。

尝试以下操作:

print("You will be charged a fee of 10 dollars. "); 

print("your account is now closed.");

准备好你的字符串,最后你可以做

result= result.replaceAll("\\\\n","");

然后
print

您必须使用
print
println
方法的组合

print
方法打印传递的数据,但是
println
方法首先打印数据,然后移动到下一行。看一看这张照片

在你的情况下,你可以这样做-

print("You will be charged a fee of 10 dollars."); 


您可以准备“显示”语句,然后打印一次。但是,这种方法看起来不太好,因为您正在打印
readLine
方法中的
是否要关闭帐户?

您必须使用print而不是println

println类似于print,但在字符串的末尾添加了一个“\n”,即换行符

如果要生成多行,也可以使用“打印”并在需要中断的位置添加“\n”

例如:

print("This is an example with one line")
print("This is still in the same line as the text ahead.")
如果你想做两行,但有一个字符串,你也可以

print("This is an example with 2 lines" + "\n" + "but only 1 String")
println会使行末尾的“\n”像以前一样

println("This is an example with two lines") // Here is an automatically added "\n"
print ("This text is in the next line")

换行符是什么?@Jessica M。它是终端在下一行写入下一个字符的信号。您将在代码中看到它为“\n”。println会自动为您输入的字符串加上一个后缀。打印没有。你能编辑一下你的答案吗?我在手机上错误地投了反对票,而不是投了赞成票。现在它说,我可以向上投票,只有当你编辑它。:)完成。电话里两个人太近了。所以错误地点击了下一个。现在更正它。
println("This is an example with two lines") // Here is an automatically added "\n"
print ("This text is in the next line")