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;//保證修改任意輸入值時,不顯示計算結果;

 

         

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM