简介:
为银行存款客户提供一个超级计算器,简单直观操作界面,提供一个银行本意到期金额结算查询程序,方便用户选择存款方式。
功能截图:
实验步骤:利用工具栏建造窗体设计如图;
1.建立2个GroupBox控件,左侧GroupBox放入四个label标签,分别表明“存款金额(元),年利率(%),存期(年),利息计算方式,”
2 放入3个TextBox分别对应“存款金额(元),年利率(%),存期(年)”,将其属性 Name 改为“textBoxstartAmount,textBoxYearRate,textBoxYears”
3.放入 ComboBox下拉框对应利息计算方式,属性“Name”改为“comboBoxCalculateType”
4.放入button控件,属性 text改为“计算”,Name改为“buttonOK”
5.右侧放入2个label,属性Name 改为“labelParameter,labelResult”
6.进行代码编写“:
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 Superalculator2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
string[] caclType = { "按月计算","按季度计算","按年计算"};
comboBoxCalculateType.Items.AddRange(caclType);
comboBoxCalculateType.SelectedIndex = ;
labelResult.Text = string.Empty;//保证修改任意输入值时,不显示计算结果
} private void Form1_Shown(object sender,EventArgs e)
{
textBoxStarAmount.Focus();
} private void buttonOK_Click(object sender, EventArgs e)
{
//存款金额
int startAmount; //年利率 float yearRate; //存期
int years;
if(!ConvertStringToNumber(textBoxStarAmount .Text,true ,out startAmount ) )
{
MessageBox.Show("存款金额输入有错", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
} if(startAmount <)
{
MessageBox.Show("存款金额不得少于100元","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
if(ConvertStringToNumber(textBoxYearRate.Text ,true ,out yearRate )== false ) {
MessageBox.Show("年利率输入有错","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning );
return; }
yearRate /= 100.0f;
if(ConvertStringToNumber (textBoxYears .Text ,true ,out years)== false )
{
MessageBox.Show("存期(年)输入有误","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
} if (comboBoxCalculateType.SelectedIndex == -) {
MessageBox.Show("请选择提供的利息计算方式","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
labelParameter.Text =
string.Format("存款金额:{0}元{3}{3}年利率:{1}%{3}{3}存期:{2}年",
startAmount ,yearRate *,years,Environment .NewLine ); //labelResult.Text = string.Format("到期结算结果:{0:F2}元", Caculate(startAmount, yearRate / 12, years * 12));
switch (comboBoxCalculateType.SelectedItem.ToString())
{
case "按月计算":
labelResult.Text = string.Format("到期结算金额;{0:F2}元",
Caculate(startAmount, yearRate / , years * ) );
break ;
case "按季度计算":
labelResult.Text = string.Format("到期结算金额{0:F2}元",
Caculate(startAmount, yearRate / , years * ));
break;
case "按年计算":
labelResult.Text = string.Format("到期结算金额{0:F2}元",
Caculate(startAmount, yearRate, years));
break;
}
}
private void groupBox1_Enter(Object sender ,EventArgs e)
{
labelParameter.Text = string.Empty;
labelResult.Text = string.Empty;
} private float Caculate(int startAmount, float rate,int count)
{
//throw new NotImplementedException();
float total = startAmount;
for (int i = ; i <= count; i++)
{
total += total * rate;
}
return total;
} /// <summary>
/// 将字符串转化为32位整数
/// </summary>
/// <param name="s">被被转化的字符</param>
/// <param name="mustGreatThanZero">是否必须大于0的要求</param>
/// <param name="result">转化后的结果</param>
/// <returns></returns> private bool ConvertStringToNumber(string s, bool mustGreatThanZero, out int result)
{
// throw new NotImplementedException();
if (int.TryParse(s, out result) == false)
{
return false;
}
else if (mustGreatThanZero && result <= )
{
return false;
}
return true;
}
/// <summary>
/// 将字符串转化为32位整数
/// </summary>
/// <param name="s">被被转化的字符</param>
/// <param name="mustGreatThanZero">是否必须大于0的要求</param>
/// <param name="result">转化后的结果</param>
/// <returns></returns> private bool ConvertStringToNumber(string s,bool mustGreatThanZero,out float result)
{
if (float.TryParse(s, out result) == false)
{
return false;
}
if (mustGreatThanZero && result <= )
{
return false;
}
return true;
}
实验总结:实验参考书籍《C#程序设计上机指导与实例解析》 。
知识点:switch语句应用,字符串转化为整数。swlectedItem 调用下拉框内容,Message。Show("",""MessageBoxButtons.OK,MessageBoxIcon.Warning)警告提示语句;labelResult.Text = string.Empty;//保证修改任意输入值时,不显示计算结果;