代碼如下:
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 demo
{
public partial class Hello : Form
{
public Hello()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) // 窗體載入
{
this.Text = "判斷分數等級"; // 設定标題欄
this.Width = 600; // 設定寬度
this.Height = 300; // 設定高度
}
private void btnOK_Click(object sender, EventArgs e) // 執行求開平方
{
int inNum = int.Parse(textBox1.Text); // 讀取輸入字元串解析為數字
Judge j = new Judge(); // 建立一個Judge對象
switch (j.Level(inNum)) // j.Level(inNum)調用類的方法擷取傳回ABCD
{
case "A":
textBox2.Text = "優秀";
break;
case "B":
textBox2.Text = "良好";
break;
case "C":
textBox2.Text = "及格";
break;
case "D":
textBox2.Text = "不及格";
break;
default:
textBox2.Text = "你的成績有誤";
break;
}
}
private void btnCancle_Click(object sender, EventArgs e) // 重置
{
textBox1.Text = "";
textBox2.Text = "";
}
}
class Judge // 判斷分數等級,傳回ABCD
{
public string Level(int num)
{
if (num >= 85)
return "A";
else if (75 <= num && num < 85)
return "B";
else if (60 <= num && num < 75)
return "C";
else return "D";
}
}
}
效果圖: