我正在尝试制作一个程序,允许用户在考虑贷款时确定不同的方面。第一个方程应该确定在给定贷款金额、利率和月数的情况下每月还款额是多少。第二个方程应该确定在给定贷款金额、利率和每月还款额的情况下,一个人必须支付多少笔付款(或几个月)。这是我正在测试的代码,但我似乎无法获得有效的输出。
float amount = 20000;
float rate = (float) 7.5;
float months = 60;
float payment = 450;
float answer = (float) (((amount*(rate/1200))*(1+(Math.pow((rate/1200), months))))/((1+Math.pow((rate/1200), months))-1));
System.out.println(answer);
float answer2 = (log(payment/amount)-log((payment/amount)-(rate/1200)))/(log(1+(rate/1200)));
System.out.println(answer2);
对于第一个方程,我首先不断收到错误,因为它希望答案是双精度型,并不断告诉我,对于我的速率变量,我无法从双精度型转换为浮点型,所以我必须进行强制转换。一旦错误消失,我就会不断地得到答案。
对于第二个方程,它一直说我不能除以零。有谁知道这个问题的任何解决方案。在网上查看时,似乎我正确地制定了方程式,所以我不知道如何使它们发挥作用。
另外,如果有人碰巧知道确定利率的好公式,那将会非常有帮助。
I'm trying to make a program that will allow the user to determine different aspects when considering a loan. The first equation is supposed to determine what the monthly payment would be given the loan amount, interest rate, and number of months. The second equation is supposed to determine how many payments(or months) one would have to make given the loan amount, interest rate, and monthly payment. This is the code I'm testing with, but I can't seem to get valid output.
float amount = 20000;
float rate = (float) 7.5;
float months = 60;
float payment = 450;
float answer = (float) (((amount*(rate/1200))*(1+(Math.pow((rate/1200), months))))/((1+Math.pow((rate/1200), months))-1));
System.out.println(answer);
float answer2 = (log(payment/amount)-log((payment/amount)-(rate/1200)))/(log(1+(rate/1200)));
System.out.println(answer2);
For the first equation, I first kept getting errors because it wanted the answer to be a double and kept telling me that for my rate variable I couldn't convert from double to float, so I had to put in casts. Once I got the errors to go away, I keep getting infinity for an answer.
For the second equation, it keeps saying I can't divide by zero. Does anybody know any solutions to this. When looking online, it seems as though I'm formulated my equations correctly, so I don't know how to make them work.
Also, if anyone would happen to know a good formula for determining the interest rate, that would be so helpful.
发布评论
评论(3)
您缺少一些转换,
log
应该是Math.log
。当我将所有内容都转换为双精度时(为什么你如此坚持使用浮点数?使用双精度意味着你不需要将这些转换放在任何地方)我得到以下输出:不确定第二个值应该是什么,但这似乎没问题;没有警告,也没有错误。关于第一部分,您要除以零。试试这个:
你会得到以下微小的数字
浮点运算将导致分母为零,因为该值 +1 将失去精度,因此:
产生
0.0
并且分母将导致第二个结果为无穷大。You're missing some casts, and
log
should beMath.log
. When I convert everything to double (why are you so insistent on using floats? Using doubles means you won't need to put those casts everywhere) I get the following output:Not sure what the second value is supposed to be, but that seems ok; no warnings and no errors. Regarding the first part, you're dividing by zero. Try this:
And you'll get the following tiny tiny number
Floating point arithmetic will result in your denominator being zero, since that value +1 will lose precision, thus:
Yields
0.0
and your denominator will cause the second result to be infinity.您不应使用 float/double 或原始整数作为货币值,而应使用 BigDecimal: http://download.oracle.com/javase/1.4.2/docs/api/java/math/BigDecimal.html
这使得没有必要强制这样做double-> float 强制转换,我希望错误消失。如果没有,请添加您的新代码的评论。
You should not use float/double or raw integers for monetary values, use BigDecimal instead: http://download.oracle.com/javase/1.4.2/docs/api/java/math/BigDecimal.html
This makes it unnecessary to do that forced double->float cast and i hope the errors disappear. If not then add a comment with your new code please.
对于第二个方程:
唯一可能被零除的情况是
log(1+(rate/1200))
等于0
。如果
(rate/1200)
返回0
,则这是可能的。您可以通过将这两种情况的(rate/1200)
转换为浮点数来解决此问题。因此:
我认为错误在于
rate/1200
始终为0
因为1200
是一个整数,因此分配。For the second equation:
The only possible division by zero is if
log(1+(rate/1200))
equals0
.This is possible if
(rate/1200)
returns0
. You might solve this by casting(rate/1200)
to a float for both occurences.Thus:
I think the error lies in the fact that
rate/1200
is always0
because1200
is an integer and so is the solution of the division.