有没有一种方法可以分解一行代码,使其在 java 中即使在新行中也能被读取为连续的?
public String toString() {
return String.format("BankAccount[owner: %s, balance: %2$.2f,\
interest rate: %3$.2f,", myCustomerName, myAccountBalance, myIntrestRate);
}
上面的代码当我在一行中执行所有这些时一切正常,但是当我尝试在多行中执行此操作时它不起作用。
在 python 中,我知道您使用\开始在新行上键入,但在执行时打印为一行。
一个 Python 示例来说明。在 python 中,这将打印在一行上使用 反斜杠或 ():
print('Oh, youre sure to do that, said the Cat,\
if you only walk long enough.')
用户会认为这是:
Oh, youre sure to do that, said the Cat, if you only walk long enough.
Java 中有类似的方法吗?谢谢!
最佳答案
使用 +
运算符在新行上拆分字符串。
public String toString() {
return String.format("BankAccount[owner: %s, balance: "
+ "%2$.2f, interest rate:"
+ " %3$.2f]",
myCustomerName,
myAccountBalance, myIntrestRate);
}
示例输出:BankAccount[所有者:TestUser,余额:100.57,利率:12.50]
关于java - 将字符串文字拆分成多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43508087/