2016-10-01 50 views
-2

我想知道爲什麼會出現錯誤,如何修復它爲我的Java項目。DecimalFormat.format方法調用:不兼容的類型

我不得不做出完全一樣了,因爲這些:

  • 的年利率什麼是爲小數? (例如:0.045).033
  • 多少年內將你的抵押貸款在哪裏舉行? 15
  • 你借了多少抵押貸款? 300000
  • 數0.033可表示爲3.3%
  • 抵押貸款金額爲$ 300,000.00
  • 以美元每月支付$ 2,115.30
  • 超過以美元年付款總額是$ 380,754.76
  • 過度 - 付款是$ 80,754.76超額付款的比例爲 按揭是26.9

這就是我在Eclipse上所做的;

double annIntRat; 
    int nOY; 
    int borrowMor; 
    int M; 
    double monthPay; 
    double mIR; 

    Scanner scnr = new Scanner(System.in); 

    // Your code should go below this line 
    System.out.print("What is your annual interest rate as a decimal? (ex 0.045): "); 
    annIntRat = scnr.nextDouble(); 
    System.out.print("How many years will your mortgage be held? "); 
    nOY = scnr.nextInt(); 
    System.out.print("What amount of the mortgage did you borrow? "); 
    borrowMor = scnr.nextInt();  
    DecimalFormat df = new DecimalFormat("0.0"); 
    System.out.println("\nThe number "+annIntRat+" can be represented as "+df.format((annIntRat)*100)+"%"); 
    NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(); 
    M=defaultFormat.format(borrowMor); //< Here is the error and tells me to change to String.But if I do so, there will be an error down there for monthPay=..... 
    System.out.println("The mortgage amount is "+M); 
    mIR=(annIntRat)/12; 
    monthPay=(mIR * M)/(1-(1/Math.pow(1+mIR,12*nOY))); 
+3

可否請你指出什麼問題呢? –

+0

有一個旁邊的「M = defaultFormat.format(borrowMor)」 –

回答

0

我花了一段時間纔看到您突出顯示錯誤的位置,我建議您更明確地指出錯誤的位置。

的NumberFormat的「格式」的方法使用的是回報String類型的,這可以解釋你的錯誤。

下應該做的伎倆,雖然你不能肯定的是,用戶要輸入一個整數......拿這一點。

M = Integer.parseInt(defaultFormat.format(borrowMor)); 
0

DecimalFormat.format(long)方法是從NumberFormat類繼承的方法 - NumberFormat.format(long)。該方法返回String的實例。

所以,僅僅使用String類型的實例存儲和使用方法的返回值:

String borrowMorString = defaultFormat.format(borrowMor); 
System.out.println("The mortgage amount is " + borrowMorString); 
// … 
monthPay = (mIR * borrowMor)/(1 - (1/Math.pow(1 + mIR, 12 * nOY))); 
+0

我曾嘗試這一點,但隨後這一部分。 monthPay =(mIR * M)/(1-(1/Math.pow(1 + mIR,12 * nOY)));將會有錯誤提示「操作*未定義爲double,string」。 –

+0

@YeChanPark,請參閱更新。對於'monthPay'分配,使用'borrowMor'而不是'M'('M'變量應該被刪除爲冗餘)。 –