VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > C#编程 >
  • C#教程之房贷计算器代码2.0

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

复制代码
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using System.Drawing;
  7 using System.Reflection;
  8 using System.Net.NetworkInformation;
  9 
 10 namespace play
 11 {
 12     class Program
 13     {
 14         //短期利率与长期利率
 15         double lowRate = 0;
 16         double highRate = 0;
 17 
 18         //KeyValuePair<DateTime, >;
 19         DateTime dt1 = new DateTime(2015, 3, 1);
 20         DateTime dt2 = new DateTime(2015, 5, 11);
 21         DateTime dt3 = new DateTime(2015, 6, 28);
 22         DateTime dt4 = new DateTime(2015, 8, 26);
 23         //贷款金额
 24         static double totalMoney, balance;
 25 
 26         static string choice;
 27         static string newChoice;
 28         //期数
 29         int n;
 30         //月供
 31         double payMonth;
 32         double rate;
 33         double interest;
 34         /// <summary>
 35         /// 用来构建欢迎界面
 36         /// </summary>
 37         void Paint()
 38         {
 39             Console.WriteLine("***************************");
 40             Console.WriteLine("***************************");
 41             Console.WriteLine("***************************");
 42             Console.WriteLine("欢迎使用房贷计算器");
 43             Console.WriteLine("***************************");
 44             Console.WriteLine("***************************");
 45             Console.WriteLine("***************************");
 46 
 47             Console.WriteLine("请按任意键继续");
 48             Console.ReadKey();
 49             Console.Clear();
 50         }
 51         /// <summary>
 52         /// 按日期选择利率
 53         /// </summary>
 54         /// <param name="dt">输入的日期</param>
 55         void RateChoose(DateTime dt)
 56         {
 57             if (dt >= dt4)
 58             {
 59                 lowRate = 0.0275;
 60                 highRate = 0.0325;
 61             }
 62             else if (dt >= dt3)
 63             {
 64                 lowRate = 0.03;
 65                 highRate = 0.035;
 66             }
 67             else if (dt >= dt2)
 68             {
 69                 lowRate = 0.0325;
 70                 highRate = 0.0375;
 71             }
 72             else if (dt >= dt1)
 73             {
 74                 lowRate = 0.035;
 75                 highRate = 0.04;
 76             }
 77             else
 78             {
 79                 Console.WriteLine("你输入的日期不在范围内");
 80                 Console.ReadKey();
 81                 Environment.Exit(0);
 82             }
 83         }
 84         /// <summary>
 85         /// 计算月供
 86         /// </summary>
 87         void Payment(DateTime dt)
 88         {
 89             //设置缓冲区高度,即设置显示的最大行数
 90             Console.BufferHeight = 1000;
 91             RateChoose(dt);
 92             switch (choice)
 93             {
 94                 case "1":
 95                     {
 96                         //等额本息,月供计算方法
 97                         for (n = 1; n <= 360; n++)
 98                         {
 99 
100 
101                             if (n <= 60)
102                             {
103                                 rate = lowRate;
104                             }
105                             else
106                             {
107                                 rate = highRate;
108                             }
109                             //计算月供
110                             double p;
111                             p = rate / 12;
112                             payMonth = (p * totalMoney * Math.Pow((1 + p), n)) / ((Math.Pow(1 + p, n) - 1));
113                             //利息
114                             interest = n * payMonth;
115                             if (n % 2 == 0)
116                             {
117                                 Console.ForegroundColor = ConsoleColor.Blue;
118                             }
119                             else
120                             {
121                                 Console.ForegroundColor = ConsoleColor.White;
122                             }
123                             Console.WriteLine("{0}期按揭贷款的月供是{1:0.00}元,总利息是{2:0.00}元", n, payMonth, interest);
124 
125                         }
126                     }
127                     break;
128                 case "2":
129                     {
130                         //等额本金,月供计算方法
131                         for (n = 1; n <= 360; n++)
132                         {
133                             if (n <= 60)
134                             {
135                                 rate = lowRate;
136                             }
137                             else
138                             {
139                                 rate = highRate;
140                             }
141                             //计算月供
142                             double p;
143                             p = rate / 12;
144                             payMonth = (totalMoney / n) + totalMoney * p;
145                             interest = ((totalMoney / n + totalMoney * p) + totalMoney / n * (1 + p)) / 2 * n - totalMoney;
146                             Console.WriteLine("{0}期按揭贷款的首月还款额是{1:0.00}元,总利息是{2:0.00}元", n, payMonth, interest);
147                         }
148                     }
149                     break;
150             }
151         }
152 
153         /// <summary>
154         /// 月贷计算方法
155         /// </summary>
156         /// <param name="n">还款期数</param>
157         void Compute(int n)
158         {
159             Console.WriteLine("期次\t\t偿还利息\t\t偿还本金\t\t还款额\t\t剩余本金");
160             switch (choice)
161             {
162 
163                 case "1":
164                     //等额本息法计算房贷
165                     {
166                         if (n <= 60)
167                         {
168                             rate = lowRate;
169                         }
170                         else
171                         {
172                             rate = highRate;
173                         }
174 
175                         double p;
176                         p = rate / 12;
177                         payMonth = (p * totalMoney * Math.Pow((1 + p), n)) / ((Math.Pow(1 + p, n) - 1));
178                         for (int i = 1; i <= n; i++)
179                         {
180                             //string s = i.ToString();
181                             interest = totalMoney * p;
182                             double capital = payMonth - interest;
183                             totalMoney -= capital;
184                             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);
185                         }
186                     }
187                     break;
188                 case "2":
189                     //等额本金法计算房贷
190                     {
191                         if (n <= 60)
192                         {
193                             rate = lowRate;
194                         }
195                         else
196                         {
197                             rate = highRate;
198                         }
199                         double p;
200                         p = rate / 12;
201                         double capital = totalMoney / n;
202                         for (int i = 1; i <= n; i++)
203                         {
204 
205                             interest = totalMoney * p;
206                             payMonth = capital + interest;
207                             totalMoney -= capital;
208 
209                             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);
210                         }
211                     }
212                     break;
213 
214             }
215         }
216         /// <summary>
217         /// 计算还款状况
218         /// </summary>
219         /// <param name="n0">还款期数</param>
220         /// <param name="dt">首次还款时间</param>
221         void Repayment(int n0, DateTime dt)
222         {
223             balance = totalMoney;
224             DateTime dt0 = dt;
225             double rate0;
226             double capital;
227             double p;
228             Console.WriteLine("还款时间\t\t偿还利息\t\t偿还本金\t\t还款额\t\t剩余本金");
229             switch (choice)
230             {
231                 case "1":
232                     {
233                         
234                         RateChoose(dt0);
235                         if (n0 <= 60)
236                         {
237                             rate0 = lowRate;
238                         }
239                         else
240                         {
241                             rate0 = highRate;
242                         }
243                         n = n0;
244                         for (int i = 1; i <=n0; i++)
245                         {
246                             RateChoose(dt);
247                             rate = (n0 <= 60 ? lowRate : highRate);
248                             
249                             if (dt.Year != dt0.Year && rate != rate0)//这里应该有问题?
250                             {
251                                 double p0 = rate0 / 12;
252                                 double payMonth0= (p0 * totalMoney * Math.Pow((1 + p0), n)) / ((Math.Pow(1 + p0, n) - 1));
253                                 double interest0 = balance * p0;
254                                 double capital0 = payMonth0 - interest0;
255                                 
256                                 rate0 = rate;
257                                
258                                 interest = balance * (rate0/12);
259                                 totalMoney = balance;
260                                 balance -= capital0;
261                                
262                                 payMonth = capital0 + interest;
263                                 n =n0 - i+1;
264                                 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);
265                                 dt = dt.AddMonths(1);
266                                 continue;
267                             }
268 
269 
270                            
271                             p = rate0 / 12;
272                             payMonth = (p * totalMoney * Math.Pow((1 + p), n)) / ((Math.Pow(1 + p, n) - 1));
273 
274                             interest = balance * p;
275                              capital = payMonth - interest;
276                             balance -= capital;
277                             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);
278                             dt = dt.AddMonths(1);
279 
280                         }
281 
282                     }
283                     break;
284                 case "2":
285                     {
286                         RateChoose(dt0);
287                         rate0 = (n0 <= 60 ? lowRate : highRate);
288                         n = n0;
289                         for (int i = 0; i < n0; i++)
290                         {
291                             RateChoose(dt);
292                             rate = (n0 <= 60 ? lowRate : highRate);
293                             if (dt.Year!=dt0.Year&&rate!=rate0)
294                             {
295                                 rate0 = rate;
296                             }
297                             p = rate0 / 12;
298                             capital = totalMoney / n0;
299                             interest = balance * p;
300                             payMonth = capital + interest;
301                             balance -= capital;
302                             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);
303                             dt = dt.AddMonths(1);
304                         }
305                     }
306                     break;
307             }
308         }
309         /// <summary>
310         ///实现提前还款功能
311         /// </summary>
312         /// <param name="n">还款总期数</param>
313         /// <param name="dt1">首次还款时间</param>
314         /// <param name="dt2">提前还款时间</param>
315         void Prepayment(int n0,DateTime dt1,DateTime dt2,double money)
316         {
317             balance = totalMoney;
318             DateTime dt0 = dt1;
319             double rate0;
320             double capital;
321             double p;
322             bool flag = false;
323             Console.WriteLine("还款时间\t\t偿还利息\t\t偿还本金\t\t还款额\t\t剩余本金");
324             switch (choice)
325             {
326                 case "
      



  
相关教程