2017-02-15 15 views

回答

1

您的级联结束前取出分号。

String output = "If you borrow" + currencyFormatter.format(loanAmount) 
    +" at an interest rate of" + rate + "%" 
    +"\nfor" + years 
    +",you will pay" + totalInterest + "in interest."; 

我还建议您将连接运算符移动到行的末尾而不是行的开始位置。这是一种较小的文体偏好...

String output = "If you borrow" + currencyFormatter.format(loanAmount) + 
    " at an interest rate of" + rate + "%" + 
    "\nfor" + years + 
    ",you will pay" + totalInterest + "in interest."; 

最后,您可能会注意到,当您尝试打印该字符串时缺少一些空格。 String.format方法对此有帮助(另请参阅Formatter的文档)。它比做大量的连接还要快。

String output = String.format(
    "If you borrow %s at an interest rate of %d%%\nfor %d years, you will pay %d in interest.", currencyFormatter.format(loanAmount), rate, years, totalInterest 
); 
相关问题