c#银行本息和计算器
程序员文章站
2022-05-06 09:29:37
...
先看运行结果:
控件好摆,主要代码如下:
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 Calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) //当用户点击时显示计算结果
{
try
{
string str01 = this.principal.Text.Trim();
string str02 = this.rate.Text.Trim();
string str03 = this.loanPeriod.Text.Trim();
double p = Convert.ToDouble(str01);
double r = Convert.ToDouble(str02) / 100;
double t = Convert.ToDouble(str03);
if (this.simple.Checked)
{
//单利计算公式
this.totalAmount.Text = Convert.ToString(p + p * r * t);
}
if (this.compound.Checked)
{
//复利计算公式
this.totalAmount.Text = Convert.ToString(p * Math.Pow(1 + r, t));
}
}
catch
{
MessageBox.Show("输入为空或非法字符,请重新输入!","错误");
}
}
private void simple_CheckedChanged(object sender, EventArgs e)
{
try
{
string str01 = this.principal.Text.Trim();
string str02 = this.rate.Text.Trim();
string str03 = this.loanPeriod.Text.Trim();
double p = Convert.ToDouble(str01);
double r = Convert.ToDouble(str02) / 100;
double t = Convert.ToDouble(str03);
if (this.simple.Checked)
{
this.totalAmount.Text = Convert.ToString(p + p * r * t);
}
if (this.compound.Checked)
{
this.totalAmount.Text = Convert.ToString(p * Math.Pow(1 + r, t));
}
}
catch
{
MessageBox.Show("输入为空或非法字符,请重新输入!","错误");
}
}
private void button2_Click(object sender, EventArgs e) //点击使所有文本框清空且默认选择简单计算方式
{
this.simple.Checked = true;
this.compound.Checked = false;
this.principal.Text = null;
this.rate.Text = null;
this.loanPeriod.Text = null;
this.totalAmount.Text = null;
}
private void button3_Click(object sender, EventArgs e)
//使程序退出
{
Application.Exit();
}
}
}
早期写的代码,习惯还不太好…………见谅啊大佬们!
推荐阅读