c#0银行存款计算器

简介:

      为银行存款客户提供一个超级计算器,简单直观操作界面,提供一个银行本意到期金额结算查询程序,方便用户选择存款方式。

功能截图:

实验步骤:利用工具栏建造窗体设计如图;

              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.进行代码编写“:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 
 11 namespace Superalculator2
 12 {
 13     public partial class Form1 : Form
 14     {
 15         public Form1()
 16         {
 17             InitializeComponent();
 18             this.StartPosition = FormStartPosition.CenterScreen;
 19             string[] caclType = { "按月计算","按季度计算","按年计算"};
 20             comboBoxCalculateType.Items.AddRange(caclType);
 21             comboBoxCalculateType.SelectedIndex = 0;
 22             labelResult.Text = string.Empty;//保证修改任意输入值时,不显示计算结果
23 } 24 25 private void Form1_Shown(object sender,EventArgs e) 26 { 27 textBoxStarAmount.Focus(); 28 } 29 30 private void buttonOK_Click(object sender, EventArgs e) 31 { 32 //存款金额 33 int startAmount; 34 35 //年利率 36 37 float yearRate; 38 39 //存期 40 int years; 41 if(!ConvertStringToNumber(textBoxStarAmount .Text,true ,out startAmount ) ) 42 { 43 MessageBox.Show("存款金额输入有错", "提示", 44 MessageBoxButtons.OK, MessageBoxIcon.Warning); 45 return; 46 } 47 48 if(startAmount <100) 49 { 50 MessageBox.Show("存款金额不得少于100元","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning); 51 return; 52 } 53 if(ConvertStringToNumber(textBoxYearRate.Text ,true ,out yearRate )== false ) 54 55 { 56 MessageBox.Show("年利率输入有错","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning ); 57 return; 58 59 } 60 yearRate /= 100.0f; 61 if(ConvertStringToNumber (textBoxYears .Text ,true ,out years)== false ) 62 { 63 MessageBox.Show("存期(年)输入有误","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning); 64 return; 65 } 66 67 if (comboBoxCalculateType.SelectedIndex == -1) 68 69 { 70 MessageBox.Show("请选择提供的利息计算方式","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning); 71 return; 72 } 73 labelParameter.Text = 74 string.Format("存款金额:{0}元{3}{3}年利率:{1}%{3}{3}存期:{2}年", 75 startAmount ,yearRate *100,years,Environment .NewLine ); 76 77 //labelResult.Text = string.Format("到期结算结果:{0:F2}元", Caculate(startAmount, yearRate / 12, years * 12)); 78 switch (comboBoxCalculateType.SelectedItem.ToString()) 79 { 80 case "按月计算": 81 labelResult.Text = string.Format("到期结算金额;{0:F2}元", 82 Caculate(startAmount, yearRate / 12, years * 12) ); 83 break ; 84 case "按季度计算": 85 labelResult.Text = string.Format("到期结算金额{0:F2}元", 86 Caculate(startAmount, yearRate / 4, years * 4)); 87 break; 88 case "按年计算": 89 labelResult.Text = string.Format("到期结算金额{0:F2}元", 90 Caculate(startAmount, yearRate, years)); 91 break; 92 } 93 } 94 private void groupBox1_Enter(Object sender ,EventArgs e) 95 { 96 labelParameter.Text = string.Empty; 97 labelResult.Text = string.Empty; 98 } 99 100 private float Caculate(int startAmount, float rate,int count) 101 { 102 //throw new NotImplementedException(); 103 float total = startAmount; 104 for (int i = 1; i <= count; i++) 105 { 106 total += total * rate; 107 } 108 return total; 109 } 110 111 112 113 /// <summary> 114 /// 将字符串转化为32位整数 115 /// </summary> 116 /// <param name="s">被被转化的字符</param> 117 /// <param name="mustGreatThanZero">是否必须大于0的要求</param> 118 /// <param name="result">转化后的结果</param> 119 /// <returns></returns> 120 121 private bool ConvertStringToNumber(string s, bool mustGreatThanZero, out int result) 122 { 123 // throw new NotImplementedException(); 124 if (int.TryParse(s, out result) == false) 125 { 126 return false; 127 } 128 else if (mustGreatThanZero && result <= 0) 129 { 130 return false; 131 } 132 return true; 133 } 134 /// <summary> 135 /// 将字符串转化为32位整数 136 /// </summary> 137 /// <param name="s">被被转化的字符</param> 138 /// <param name="mustGreatThanZero">是否必须大于0的要求</param> 139 /// <param name="result">转化后的结果</param> 140 /// <returns></returns> 141 142 private bool ConvertStringToNumber(string s,bool mustGreatThanZero,out float result) 143 { 144 if (float.TryParse(s, out result) == false) 145 { 146 return false; 147 } 148 if (mustGreatThanZero && result <= 0) 149 { 150 return false; 151 } 152 return true; 153 }

 实验总结:实验参考书籍《C#程序设计上机指导与实例解析》 。

              知识点:switch语句应用,字符串转化为整数。swlectedItem 调用下拉框内容,Message。Show("",""MessageBoxButtons.OK,MessageBoxIcon.Warning)警告提示语句;labelResult.Text = string.Empty;//保证修改任意输入值时,不显示计算结果;

         

原文地址:https://www.cnblogs.com/gdp176119/p/5966450.html