我正在爲貸款計算器工作。目標是根據客戶信用評分改變利率。如果(finalScore < = 550)工作正常,並打印monthlyPayment到像我想要的2個小數位。我遇到的問題是,當我去到下一條if語句「if(finalScore> 550 & & finalScore < = 680)」它會錯誤地打印每月付款。就像一個例子,輸出看起來像147.04938.19。無法在if語句的第二級之後將輸出打印到右邊的小數位
我試着將我的printf中的%.2f改爲%.2d,它解決了這個問題,但拋出了一個非法的格式異常。
此處有任何建議嗎?正確的輸出的
if(finalScore <= 550) {}
if (mortgage.equals(mlType)){}
if (lYears == 30) {
double interestRate = mIR550[0]/100 + pRate;
double monthlyRate = interestRate/12.0;
int termInMonths = 360;
double monthlyPayments = ((mlAmount*monthlyRate)
/(1-Math.pow(1+monthlyRate, -termInMonths)));
System.out.printf("%.2f", monthlyPayments);
//prints fine here in format xxx.xx
舉例:壞輸出
Please enter Credit Score:
680
Loan Type Mortgage:
Mortgage
Enter Years:
30
Enter Loan Amount:
20000
___________________________________
147.03125.10
BUILD SUCCESSFUL (total time: 30 seconds)
代碼
Please enter Credit Score:
550
Loan Type Mortgage:
Mortgage
Enter Years:
30
Enter Loan Amount:
20000
___________________________________
147.03
BUILD SUCCESSFUL (total time: 2 minutes 24 seconds)
例子,其中的問題是發生
if(finalScore >550 && finalScore <=680) {
if (mortgage.equals(mlType)){
if (lYears == 30) {
double interestRate = mIR680[0]/100 + pRate;
double monthlyRate = interestRate/12.0;
int termInMonths = 360;
double monthlyPayments = ((mlAmount*monthlyRate)
/(1-Math.pow(1+monthlyRate, -termInMonths)));
System.out.printf("%.2f", monthlyPayments);
// output returns answer formatted xxx.xxxxx.xx not sure
why?
你可以把你的代碼的必要性,或者你可以提供一個小而清晰的例子來描述你的問題嗎? –
我試圖把它清理一下。我遇到的問題是輸出。對於第一塊代碼,它給了我正確的格式xxx.xx的輸出,但是當我運行最後一塊代碼時,我得到的輸出格式爲xxx.xxxxx.xx –