有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在多行上分解字符串文字

有没有一种方法可以分解一行代码,使其在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中有类似的方法吗??谢谢大家!


共 (2) 个答案

  1. # 1 楼答案

    使用+运算符在新行上拆分字符串

    public String toString() {
        return String.format("BankAccount[owner: %s, balance: "
                + "%2$.2f, interest rate:"
                + " %3$.2f]", 
                myCustomerName, 
                myAccountBalance, myIntrestRate);
    }
    

    示例输出:BankAccount[owner: TestUser, balance: 100.57, interest rate: 12.50]