所以我对编码很新,而且我只是回归0。我在运行它时输入10000 5和10作为我的3个输入,我无法从其中返回任何内容,除了0以外的所有三个字段。我想也许我的程序想要一个变量的起点,所以我将它们全部声明为0以启动它仍然无效。
int AIR, MIR, PMT, IP, PP, ABorrowed, Term;
AIR = 0;
MIR = 0;
PMT = 0;
IP = 0;
PP = 0;
ABorrowed = 0;
Term = 0;
Console.WriteLine("Please enter the amount borrowed on your loan ");
ABorrowed = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter the interest rate for your loan ");
AIR = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter term of your loan in months ");
Term = int.Parse(Console.ReadLine());
MIR = AIR / 12;
PMT = ABorrowed * (MIR/((1-(1/(1+MIR))^Term)));
IP = ABorrowed * MIR;
PP = PMT - IP;
Console.WriteLine("Your total payment for this month is " + PMT);
Console.WriteLine("Of that payment " + IP + " is interest rate");
Console.WriteLine("and the Payment Portion is " + PP);
Console.ReadLine();
答案 0 :(得分:3)
此代码中存在与您的描述相关的几个问题:
^
首先,整数除法返回一个整数,意思是:
10 / 3 = 3
如果您对类型使用decimal
而不是int
,则更有可能获得正确的结果。
此外,你在那里使用^
,我认为这是你提升某些东西的力量的方式,但^
是XOR
运算符,完全不同的东西。
要在C#/ .NET中提升某些功能,请使用Math.Pow:
PMT = ABorrowed * (MIR/((1-Math.Pow((1/(1+MIR)), Term))));
(我想想我设法将Math.Pow调用放在正确的部分周围)
答案 1 :(得分:2)
这里的核心问题是您正在使用int
。这种类型只能代表整数,对于财务计算来说非常糟糕。在使用decimal
而不是int
答案 2 :(得分:1)
使用十进制代替int而^
不是它独有的力量,或者我怀疑你想要的是什么。而是使用Math.Pow
实际上查看代码时,您可以使用var
避免一些麻烦,而不是在开始时声明所有内容。
Console.WriteLine("Please enter the amount borrowed on your loan ");
var ABorrowed = decimal.Parse(Console.ReadLine());
Console.WriteLine("Please enter the interest rate for your loan ");
var AIR = decimal.Parse(Console.ReadLine());
Console.WriteLine("Please enter term of your loan in months ");
var Term = decimal.Parse(Console.ReadLine());
var MIR = AIR / 12;
var PMT = ABorrowed * (MIR/((1-Math.Pow((1/(1+MIR)), Term))));
var IP = ABorrowed * MIR;
var PP = PMT - IP;
Console.WriteLine("Your total payment for this month is " + PMT);
Console.WriteLine("Of that payment " + IP + " is interest rate");
Console.WriteLine("and the Payment Portion is " + PP);
Console.ReadLine();
答案 3 :(得分:0)
除法运算符'/'截断到最接近零的最接近的整数,因此对MIR的赋值会破坏所有内容。
答案 4 :(得分:0)
您已将变量声明为INT,因此如果为AIR
输入5,则MIR = AIR / 12
将为零,因为5/12会转换为零。因此PMT
和IP
将为零,PP
将为零。
您应该使用decimal
,而不是int
作为变量类型。