贷款计算器C#实现 课程作业一

2021-07-20 11:14

阅读:431

标签:nbsp   convert   nta   pos   lin   str   send   event   false   

花了两个小时理解公式,我数学是不是太弱了..

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 贷款计算机
{
    public partial class Form1 : Form
    {
        int pos = 0;
        double year, cost, temp;//分别表示 年限 金额 利率
        public Form1()
        {
            InitializeComponent();
        }

        //pos == 1 表示等额本息
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox1.Checked )
            {
                checkBox2.Checked = false;
                pos = 1;
            }
            else
                pos = 0;

        }


        //pos == 2 表示等额本金
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                checkBox1.Checked = false;
                pos = 1;
            }
            else
                pos = 0;
        }

        //计算按钮
        private void button1_Click(object sender, EventArgs e)
        {
            
            try //选择还款方式
            {
                if ( pos == 0)
                    throw new Exception ();
            }
            catch (Exception)
            {
                MessageBox.Show("先选择还款方式才能进行此运算!", "错误", MessageBoxButtons.OK);
            }
            try //输入贷款年限
            {
                year = Convert.ToDouble(textBox1.Text);
                year = year * 12;//转换成贷款月份
            }
            catch (Exception)
            {
                MessageBox.Show("先输入贷款年限才能进行此运算!", "错误", MessageBoxButtons.OK);
            }

            try  //输入贷款金额
            {
                cost = Convert.ToDouble(textBox2.Text);
                cost = cost * 10000;// 贷款金额,单位元
            }
            catch (Exception)
            {
                MessageBox.Show("先输入贷款金额才能进行此运算!", "错误", MessageBoxButtons.OK);
            }

            try  //输入贷款利率
            {
                temp = Convert.ToDouble(textBox3.Text);
                temp = temp / 1200;//贷款月利率
            }
            catch (Exception)
            {
                MessageBox.Show("先输入贷款利率才能进行此运算!", "错误", MessageBoxButtons.OK);
            }
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";

            if (checkBox1.Checked)
            {
                double temp1 = cost * temp*Math.Pow(1 + temp, year+1) / (Math.Pow(1+temp,year) -1);//*temp * cost
                double temp2 = temp1 * year -cost;
                double temp3 = temp1 * year;
                textBox4.Text = temp1.ToString();
                textBox5.Text = temp2.ToString();
                textBox6.Text = temp3.ToString();
            }
            else if(checkBox2.Checked)
            {
                double temp1 = ((cost / year + cost * temp) + cost / year * (1 + temp)) / 2 ; //总利息
                double temp2 = temp1 * year - cost ;
                double temp3 = temp1 * year;
                textBox4.Text = temp1.ToString();
                textBox5.Text = temp2.ToString();
                textBox6.Text = temp3.ToString();
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            try //选择还款方式
            {
                if (pos == 0)
                    throw new Exception();
            }
            catch (Exception)
            {
                MessageBox.Show("先选择还款方式才能进行此运算!", "错误", MessageBoxButtons.OK);
            }
            try //输入贷款年限
            {
                year = Convert.ToDouble(textBox1.Text);
                year = year * 12;//转换成贷款月份
            }
            catch (Exception)
            {
                MessageBox.Show("先输入贷款年限才能进行此运算!", "错误", MessageBoxButtons.OK);
            }

            try  //输入贷款金额
            {
                cost = Convert.ToDouble(textBox2.Text);
                cost = cost * 10000;// 贷款金额,单位元
            }
            catch (Exception)
            {
                MessageBox.Show("先输入贷款金额才能进行此运算!", "错误", MessageBoxButtons.OK);
            }

            try  //输入贷款利率
            {
                temp = Convert.ToDouble(textBox3.Text);
                temp = temp / 1200;//贷款月利率
            }
            catch (Exception)
            {
                MessageBox.Show("先输入贷款利率才能进行此运算!", "错误", MessageBoxButtons.OK);
            }
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";

            if (checkBox1.Checked)
            {
                double temp1 = cost * temp * Math.Pow(1 + temp, year + 1) / (Math.Pow(1 + temp, year) - 1);//*temp * cost
                double temp2 = temp1 * year - cost;
                double temp3 = temp1 * year;
                textBox4.Text = temp1.ToString();
                textBox5.Text = temp2.ToString();
                textBox6.Text = temp3.ToString();
            }
            else if (checkBox2.Checked)
            {
                double temp1 = ((cost / year + cost * temp) + cost / year * (1 + temp)) / 2; //总利息
                double temp2 = temp1 * year - cost ;
                double temp3 = temp1 * year;
                textBox4.Text = temp1.ToString();
                textBox5.Text = temp2.ToString();
                textBox6.Text = temp3.ToString();
            }
        }
    }
}

 

贷款计算器C#实现 课程作业一

标签:nbsp   convert   nta   pos   lin   str   send   event   false   

原文地址:http://www.cnblogs.com/Draymonder/p/8038704.html


评论


亲,登录后才可以留言!