gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:53:02 26 4
gpt4 key购买 nike

现在 while 循环中的行“你将被收取 10 美元的费用”和“你想关闭你的帐户吗?”出现在单独的行上,例如...

“你将被收取10美元的费用。”
“你需要指示吗?”

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

“你将被收取 10 美元的费用。你想关闭你的帐户吗?”

import acm.program.*;

public class BankAccount extends ConsoleProgram {
public void run() {

int bankRoll = 50;

while(bankRoll > 0) {
println("Currently your balance is " + bankRoll + " . ");
println("You will be charged a fee of 10 dollars.");
String closeAct = readLine("Would you like to close your account?");
if(closeAct.equals("yes")) {
break;
}
bankroll -= 10;
} /* end of while loop */

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

}
}

最佳答案

你必须使用 print 而不是 println。

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

如果你想做多行,你也可以使用 print 并在你想休息的地方加一个“\n”。

例如:

print("This is an example with one line")
print("This is still in the same line as the text ahead.")

如果你想做2行,但是有1个String,你也可以

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")

关于java - 字符串格式 --- 将两个单独的语句组合成一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17424376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com