简介:
为银行存款客户提供一个超级计算器,简单直观操作界面,提供一个银行本意到期金额结算查询程序,方便用户选择存款方式。
功能截图:
实验步骤:利用工具栏建造窗体设计如图;
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;
19string[] caclType = { "按月计算","按季度计算","按年计算"};
20 comboBoxCalculateType.Items.AddRange(caclType);
21 comboBoxCalculateType.SelectedIndex = 0;
22 labelResult.Text = string.Empty;//保证修改任意输入值时,不显示计算结果
23 }
24 25privatevoid Form1_Shown(object sender,EventArgs e)
26 {
27 textBoxStarAmount.Focus();
28 }
29 30privatevoid buttonOK_Click(object sender, EventArgs e)
31 {
32//存款金额 33int startAmount;
34 35//年利率 36 37float yearRate;
38 39//存期 40int years;
41if(!ConvertStringToNumber(textBoxStarAmount .Text,true ,out startAmount ) )
42 {
43 MessageBox.Show("存款金额输入有错", "提示",
44 MessageBoxButtons.OK, MessageBoxIcon.Warning);
45return;
46 }
47 48if(startAmount <100)
49 {
50 MessageBox.Show("存款金额不得少于100元","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
51return;
52 }
53if(ConvertStringToNumber(textBoxYearRate.Text ,true ,out yearRate )== false )
54 55 {
56 MessageBox.Show("年利率输入有错","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning );
57return;
58 59 }
60 yearRate /= 100.0f;
61if(ConvertStringToNumber (textBoxYears .Text ,true ,out years)== false )
62 {
63 MessageBox.Show("存期(年)输入有误","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
64return;
65 }
66 67if (comboBoxCalculateType.SelectedIndex == -1)
68 69 {
70 MessageBox.Show("请选择提供的利息计算方式","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
71return;
72 }
73 labelParameter.Text =
74string.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)); 78switch (comboBoxCalculateType.SelectedItem.ToString())
79 {
80case"按月计算":
81 labelResult.Text = string.Format("到期结算金额;{0:F2}元",
82 Caculate(startAmount, yearRate / 12, years * 12) );
83break ;
84case"按季度计算":
85 labelResult.Text = string.Format("到期结算金额{0:F2}元",
86 Caculate(startAmount, yearRate / 4, years * 4));
87break;
88case"按年计算":
89 labelResult.Text = string.Format("到期结算金额{0:F2}元",
90 Caculate(startAmount, yearRate, years));
91break;
92 }
93 }
94privatevoid groupBox1_Enter(Object sender ,EventArgs e)
95 {
96 labelParameter.Text = string.Empty;
97 labelResult.Text = string.Empty;
98 }
99100privatefloat Caculate(int startAmount, float rate,int count)
101 {
102//throw new NotImplementedException();103float total = startAmount;
104for (int i = 1; i <= count; i++)
105 {
106 total += total * rate;
107 }
108return total;
109 }
110111112113///<summary>114/// 将字符串转化为32位整数
115///</summary>116///<param name="s">被被转化的字符</param>117///<param name="mustGreatThanZero">是否必须大于0的要求</param>118///<param name="result">转化后的结果</param>119///<returns></returns>120121privatebool ConvertStringToNumber(string s, bool mustGreatThanZero, outint result)
122 {
123// throw new NotImplementedException();124if (int.TryParse(s, out result) == false)
125 {
126returnfalse;
127 }
128elseif (mustGreatThanZero && result <= 0)
129 {
130returnfalse;
131 }
132returntrue;
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>141142privatebool ConvertStringToNumber(string s,bool mustGreatThanZero,outfloat result)
143 {
144if (float.TryParse(s, out result) == false)
145 {
146returnfalse;
147 }
148if (mustGreatThanZero && result <= 0)
149 {
150returnfalse;
151 }
152returntrue;
153 }
实验总结:实验参考书籍《C#程序设计上机指导与实例解析》 。
知识点:switch语句应用,字符串转化为整数。swlectedItem 调用下拉框内容,Message。Show("",""MessageBoxButtons.OK,MessageBoxIcon.Warning)警告提示语句;labelResult.Text = string.Empty;//保证修改任意输入值时,不显示计算结果;
原文:http://www.cnblogs.com/gdp176119/p/5966450.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!