您的位置:首页 > 移动开发 > 微信开发

计算复利率小程序(C#版)

2007-12-02 03:08 218 查看
程序代码如下(有许多需要修改的地方没有做好):

using System;
using System.Collections.Generic;
using System.Text;

namespace interest
{
// 计算复利率;
class interest
{
static void Main(string[] args)
{
// 定义变量;
decimal acount=0.0M; // 本金和利息;
decimal principal; // 本金;
double rate; // 年利率;
int year; // 第几年;

// 显示用户界面;
Console.WriteLine();
Console.WriteLine("欢迎使用复利率计算系统!");

// 提示用户输入参数初始化变量;
Console.WriteLine("");
Console.Write("请您输入本金金额: ");
principal = Convert.ToDecimal(Console.ReadLine());

Console.Write("请您输入年利率: ");
rate = Convert.ToDouble(Console.ReadLine());
Console.Write("请您输入计算年数: ");
year = Convert.ToInt32(Console.ReadLine());

// 显示结果表头;
Console.WriteLine("");
Console.WriteLine("=================计算结果===================");
Console.WriteLine("");
Console.WriteLine("{0,4} {1,15} {2,15}","第n年","年利","本金+年利");

// 逐条打印输出计算结果;
for (int index = 1; index <= year; index++)
{
acount = principal *((decimal) Math.Pow(1 + rate, index));
Console.WriteLine("{0,6} {1,16:C} {2,18:C}",index,acount-principal,acount);
} // end for;
} // end Main;
} // end class;
} // end namespace;

输出为:

欢迎使用复利率计算系统!

请您输入本金金额: 1000
请您输入年利率: 0.05
请您输入计算年数: 10

=================计算结果===================

第n年 年利 本金+年利
1 ¥50.00 ¥1,050.00
2 ¥102.50 ¥1,102.50
3 ¥157.63 ¥1,157.63
4 ¥215.51 ¥1,215.51
5 ¥276.28 ¥1,276.28
6 ¥340.10 ¥1,340.10
7 ¥407.10 ¥1,407.10
8 ¥477.46 ¥1,477.46
9 ¥551.33 ¥1,551.33
10 ¥628.89 ¥1,628.89

(小弟刚学C#,请勿见效,有劳各位仁兄多多指教!)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: