DecimalFormat.format方法调用:不兼容的类型
我想知道为什么会出现错误,如何修复它为我的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)));
我花了一段时间才看到您突出显示错误的位置,我建议您更明确地指出错误的位置。
的NumberFormat的“格式”的方法使用的是回报String类型的,这可以解释你的错误。
下应该做的伎俩,虽然你不能肯定的是,用户要输入一个整数......拿这一点。
M = Integer.parseInt(defaultFormat.format(borrowMor));
的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)));
我曾尝试这一点,但随后这一部分。 monthPay =(mIR * M)/(1-(1/Math.pow(1 + mIR,12 * nOY)));将会有错误提示“操作*未定义为double,string”。 –
@YeChanPark,请参阅更新。对于'monthPay'分配,使用'borrowMor'而不是'M'('M'变量应该被删除为冗余)。 –
可否请你指出什么问题呢? –
有一个旁边的“M = defaultFormat.format(borrowMor)” –