当前位置: 移动技术网 > IT编程>开发语言>c# > 房贷计算器代码

房贷计算器代码

2019年10月24日  | 移动技术网IT编程  | 我要评论

   本人尝试着利用c#语言来写一个房贷计算器控制台程序。由于刚开始学,错误与失误之处会很多。希望大家多多批评指正,我会持续改进。谢谢大家。

 

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.drawing;
using system.reflection;
using system.net.networkinformation;

namespace play
{
class program
{
//短期利率与长期利率
double lowrate = 0;
double highrate = 0;

//keyvaluepair<datetime, >;
datetime dt1 = new datetime(2015, 3, 1);
datetime dt2 = new datetime(2015, 5, 11);
datetime dt3 = new datetime(2015, 6, 28);
datetime dt4 = new datetime(2015, 8, 26);
//贷款金额
static double totalmoney, balance;

static string choice;
//期数
int n;
//月供
double paymonth;
double rate;
double interest;
/// <summary>
/// 用来构建欢迎界面
/// </summary>
void paint()
{
console.writeline("***************************");
console.writeline("***************************");
console.writeline("***************************");
console.writeline("欢迎使用房贷计算器");
console.writeline("***************************");
console.writeline("***************************");
console.writeline("***************************");

console.writeline("请按任意键继续");
console.readkey();
console.clear();
}
/// <summary>
/// 按日期选择利率
/// </summary>
/// <param name="dt">输入的日期</param>
void ratechoose(datetime dt)
{
if (dt >= dt4)
{
lowrate = 0.0275;
highrate = 0.0325;
}
else if (dt >= dt3)
{
lowrate = 0.03;
highrate = 0.035;
}
else if (dt >= dt2)
{
lowrate = 0.0325;
highrate = 0.0375;
}
else if (dt >= dt1)
{
lowrate = 0.035;
highrate = 0.04;
}
else
{
console.writeline("你输入的日期不在范围内");
console.readkey();
environment.exit(0);
}
}
/// <summary>
/// 计算月供
/// </summary>
void payment(datetime dt)
{
//设置缓冲区高度,即设置显示的最大行数
console.bufferheight = 1000;
ratechoose(dt);
switch (choice)
{
case "1":
{
//等额本息,月供计算方法
for (n = 1; n <= 360; n++)
{


if (n <= 60)
{
rate = lowrate;
}
else
{
rate = highrate;
}
//计算月供
double p;
p = rate / 12;
paymonth = (p * totalmoney * math.pow((1 + p), n)) / ((math.pow(1 + p, n) - 1));
//利息
interest = n * paymonth;
if (n % 2 == 0)
{
console.foregroundcolor = consolecolor.blue;
}
else
{
console.foregroundcolor = consolecolor.white;
}
console.writeline("{0}期按揭贷款的月供是{1:0.00}元,总利息是{2:0.00}元", n, paymonth, interest);

}
}
break;
case "2":
{
//等额本金,月供计算方法
for (n = 1; n <= 360; n++)
{
if (n <= 60)
{
rate = lowrate;
}
else
{
rate = highrate;
}
//计算月供
double p;
p = rate / 12;
paymonth = (totalmoney / n) + totalmoney * p;
interest = ((totalmoney / n + totalmoney * p) + totalmoney / n * (1 + p)) / 2 * n - totalmoney;
console.writeline("{0}期按揭贷款的首月还款额是{1:0.00}元,总利息是{2:0.00}元", n, paymonth, interest);
}
}
break;
}
}

/// <summary>
/// 月贷计算方法
/// </summary>
/// <param name="n">还款期数</param>
void compute(int n)
{
console.writeline("期次\t\t偿还利息\t\t偿还本金\t\t还款额\t\t剩余本金");
switch (choice)
{

case "1":
//等额本息法计算房贷
{
if (n <= 60)
{
rate = lowrate;
}
else
{
rate = highrate;
}

double p;
p = rate / 12;
paymonth = (p * totalmoney * math.pow((1 + p), n)) / ((math.pow(1 + p, n) - 1));
for (int i = 1; i <= n; i++)
{
//string s = i.tostring();
interest = totalmoney * p;
double capital = paymonth - interest;
totalmoney -= capital;
console.writeline("{0}\t\t{1:0.00}\t\t{2:0.00}\t{3:0.00}\t{4:0.00}", i, interest, capital, paymonth, totalmoney);
}
}
break;
case "2":
//等额本金法计算房贷
{
if (n <= 60)
{
rate = lowrate;
}
else
{
rate = highrate;
}
double p;
p = rate / 12;
double capital = totalmoney / n;
for (int i = 1; i <= n; i++)
{

interest = totalmoney * p;
paymonth = capital + interest;
totalmoney -= capital;

console.writeline("{0}\t\t{1:0.00}\t\t\t{2:0.00}\t\t{3:0.00}\t{4:0.00}", i, interest, capital, paymonth, totalmoney);
}
}
break;

}
}
/// <summary>
/// 计算还款状况
/// </summary>
/// <param name="n0">还款期数</param>
/// <param name="dt">首次还款时间</param>
void repayment(int n0, datetime dt)
{
balance = totalmoney;
datetime dt0 = dt;
double rate0;
double capital;
double p;
console.writeline("还款时间\t\t偿还利息\t\t偿还本金\t\t还款额\t\t剩余本金");
switch (choice)
{
case "1":
{

ratechoose(dt0);
if (n0 <= 60)
{
rate0 = lowrate;
}
else
{
rate0 = highrate;
}
n = n0;
for (int i = 1; i <=n0; i++)
{
ratechoose(dt);
rate = (n0 <= 60 ? lowrate : highrate);

if (dt.year != dt0.year && rate != rate0)//这里应该有问题?
{
double p0 = rate0 / 12;
double paymonth0= (p0 * totalmoney * math.pow((1 + p0), n)) / ((math.pow(1 + p0, n) - 1));
double interest0 = balance * p0;
double capital0 = paymonth0 - interest0;

rate0 = rate;

interest = balance * (rate0/12);
totalmoney = balance;
balance -= capital0;

paymonth = capital0 + interest;
n =n0 - i+1;
console.writeline("{0}\t{1:0.00}\t\t\t{2:0.00}\t\t\t{3:0.00}\t\t{4:0.00}", dt, interest, capital0, paymonth, balance);
dt = dt.addmonths(1);
continue;
}



p = rate0 / 12;
paymonth = (p * totalmoney * math.pow((1 + p), n)) / ((math.pow(1 + p, n) - 1));

interest = balance * p;
capital = paymonth - interest;
balance -= capital;
console.writeline("{0}\t{1:0.00}\t\t\t{2:0.00}\t\t\t{3:0.00}\t\t{4:0.00}", dt, interest, capital, paymonth, balance);
dt = dt.addmonths(1);

}

}
break;
case "2":
{
ratechoose(dt0);
rate0 = (n0 <= 60 ? lowrate : highrate);
n = n0;
for (int i = 0; i < n0; i++)
{
ratechoose(dt);
rate = (n0 <= 60 ? lowrate : highrate);
if (dt.year!=dt0.year&&rate!=rate0)
{
rate0 = rate;
}
p = rate0 / 12;
capital = totalmoney / n0;
interest = balance * p;
paymonth = capital + interest;
balance -= capital;
console.writeline("{0}\t{1:0.00}\t\t\t{2:0.00}\t\t\t{3:0.00}\t\t{4:0.00}", dt, interest, capital, paymonth, balance);
dt = dt.addmonths(1);
}
}
break;
}
}
/// <summary>
///实现提前还款功能
/// </summary>
/// <param name="n">还款总期数</param>
/// <param name="dt1">首次还款时间</param>
/// <param name="dt2">提前还款时间</param>
void prepayment(int n,datetime dt1,datetime dt2)
{

}
static void main(string[] args)
{

{
program p = new program();
p.paint();
console.writeline("请选择您的操作!1.房贷计算 2.月供速算 3.还款状况 4.提前还款");
console.writeline("请选择您的还款方式?1.等额本息 2.等额本金");
choice = console.readline();
console.writeline("请输入你的贷款金额");
totalmoney = convert.todouble(console.readline());
console.writeline("请输入您贷款的期数");
int number = convert.toint32(console.readline());
console.writeline("请输入你的首次贷款时间");
datetime dt = convert.todatetime(console.readline());
//console.writeline("请输入你的提前还款时间");
//datetime dtr = convert.todatetime(console.readline());
//console.writeline("请输入你的还款金额");
//double partmoney = convert.todouble(console.readline());
//console.writeline("你输入的日期是{0},短期利率是{1},长期利率是{2}",dt,p.lowrate,p.highrate);
//dt= dt.addmonths(1);
// console.writeline(dt);
p.repayment(number, dt);
// p.compute(number);
// p.payment(dtr);
console.readkey();
}
}
}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网