简单的C#抵押公式计算错误
这是我的密码:简单的C#抵押公式计算错误,c#,visual-studio-2012,C#,Visual Studio 2012,这是我的密码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MortgageApplication { class Program { static void Main(string[] args) { string
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MortgageApplication
{
class Program
{
static void Main(string[] args)
{
string input = "", year, principle, month;
double r, y, p;
bool valid = false;
y = 0;
r = 0;
p = 0;
while (valid == false)
{
Console.WriteLine("Enter the duration of the loan (Number of Years): ");
input = Console.ReadLine();
if (double.TryParse(input, out y))
{
Console.WriteLine(y);
valid = true;
}
}
valid = false;
while (valid == false)
{
Console.WriteLine("Enter the princple ammount: ");
input = Console.ReadLine();
if (double.TryParse(input, out p))
{
Console.WriteLine(p);
valid = true;
}
}
valid = false;
while (valid == false)
{
Console.WriteLine("Enter the Interest Rate ");
input = Console.ReadLine();
if (double.TryParse(input, out r))
{
valid = true;
}
}
r = r / 100;
Console.WriteLine(r);
double top = p * r / 1200;
Console.WriteLine(top);
double x = (1 + (r / 1200.0));
Console.WriteLine(x);
double n = -12 * y;
Console.WriteLine(n);
double buttom = (1 - (Math.Pow(x, n)) );
Console.WriteLine(buttom);
double solution = top / buttom;
Console.WriteLine(solution);
Console.ReadLine();
}
}
}
这应该是一个简单的抵押贷款应用程序。我有功能,但公式不正确
不确定是因为我使用了双精度编码,还是因为我的编码有问题
(pr/1200.0)/(1-(1.0+r/1200.0)^(-12.0 n))
在哪里
- p=本金(美元)
- n=年数
- r=利率(百分比)
- m=每月付款
所以我认为直接的答案是,你将速率除以100,然后再除以1200。你不应该一开始就除以100,或者以后再除以12(我喜欢第二个选项,因为它清楚地表明你说的是12个月) <> P>另外,为了减少重复代码,你可能会考虑从用户那里得到一个新的函数。比如:
private static double GetDoubleFromUser(string prompt)
{
double result;
while (true)
{
if (prompt != null) Console.Write(prompt);
var input = Console.ReadLine();
if (double.TryParse(input, out result)) break;
Console.WriteLine("Sorry, that is not a valid number. Please try again...");
}
return result;
}
现在,当您需要一个double时,只需调用此函数并传递提示字符串。这使您的代码更清晰,更易于阅读。例如,您的代码现在可以写成:
private static void Main()
{
double years = GetDoubleFromUser("Enter the duration of the loan (in years): ");
double principal = GetDoubleFromUser("Enter the princple ammount: ");
double rate = GetDoubleFromUser("Enter the interest rate: ") / 100;
Console.WriteLine("\nBased on these values entered:");
Console.WriteLine(" - Number of years .... {0}", years);
Console.WriteLine(" - Principal amount ... {0:c}", principal);
Console.WriteLine(" - Interest rate ...... {0:p}", rate);
double monthlyRate = rate / 12;
double payments = 12 * years;
double result =
principal *
(monthlyRate * Math.Pow(1 + monthlyRate, payments)) /
(Math.Pow(1 + monthlyRate, payments) - 1);
Console.WriteLine("\nYour monthly payment will be: {0:c}", result);
Console.ReadLine();
}
你的实际产出和预期产出是多少??使用所有的
控制台.WriteLine
s,是否所有内容看起来都正确,或者某些内容计算不正确?]对于这个问题,您的期望值一点也不清楚。你想要有人验证你的数学吗?也许会更好。您是否希望有人验证您的代码?你犯了什么错误?我认为你不应该把这个比率除以100。或者你的公式应该是12除以1200,我也不确定你的公式是否正确。我在网上查了一个看起来有点不同的答案,并在下面的答案中使用了它。