2017-02-15 25 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 
);