subject 클래스 함수를 하나 만들어준다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace F005_ScoreCalc
{
class Subject
{
public string Title { get; set; }
public int Credit { get; set; }
public string Grade { get; set; }
public Subject() { }
public Subject(string title, int credit)
{
this.Title = title;
this.Credit = credit;
}
}
}
Form 클래스에 코드를 짜준다.
using System;
using System.Windows.Forms;
namespace F005_ScoreCalc
{
public partial class Form1 : Form
{
private Subject[] subjects; //교과목 배열
private TextBox[] txtTitles; //과목명 TextBox의 배열
private TextBox[] txtCredits; //학점 TextBox의 배열
private ComboBox[] grades; //성적 ComboBox의 배열
public Form1()
{
InitializeComponent();
subjects = new Subject[]
{
new Subject{Title="자료구조",Credit=3},
new Subject("비주얼프로그래밍",3),
new Subject("컴퓨터구조",3),
new Subject("설계및프로젝트기본",2),
new Subject("기업가정신과리더십",2),
new Subject("중국어",2),
new Subject("인체구조와기능",2),
new Subject("중점교양",2)
};
txtTitles = new TextBox[10]
{
textBox1,textBox2,textBox3,textBox4,textBox5,
textBox6,textBox7,textBox8,textBox9,textBox10
};
txtCredits = new TextBox[10]
{
txt11,txt12,txt13,txt14,txt15,
txt16,txt17,txt18,txt19,txt20
};
grades = new ComboBox[10]
{
comboBox1,comboBox2,comboBox3,comboBox4,comboBox5,
comboBox6,comboBox7,comboBox8,comboBox9,comboBox10
};
for (int i = 0; i < subjects.Length; i++)
{
txtTitles[i].Text = subjects[i].Title;
txtCredits[i].Text = subjects[i].Credit.ToString(); //ToString 어떤 값도 문자열로 바꿔줌
}
foreach (var cb in grades)
{
cb.Items.Add("A+");
cb.Items.Add("A0");
cb.Items.Add("B+");
cb.Items.Add("B0");
cb.Items.Add("C+");
cb.Items.Add("C0");
cb.Items.Add("D+");
cb.Items.Add("D0");
cb.Items.Add("F");
}
}
private void btnClac_Click_1(object sender, EventArgs e)
{
Double totalScore = 0;
int totalCresits = 0;
for (int i = 0; i < grades.Length; i++)
{
//만약 학점 콤보박스의 값이 선택되었다면
if (grades[i].SelectedItem != null)
{
int crd = int.Parse(txtCredits[1].Text);
totalCresits += crd;
totalScore += crd * GetGrade(grades[1].SelectedItem.ToString());
}
}
txtResult.Text = (totalScore / totalCresits).ToString("0.00");
}
private double GetGrade(string v)
{
if (v == "A+") return 4.5;
else if (v == "A0") return 4.0;
else if (v == "B+") return 3.5;
else if (v == "B0") return 3.0;
else if (v == "C+") return 2.5;
else if (v == "C0") return 2.0;
else if (v == "D+") return 1.5;
else if (v == "D0") return 1.0;
else return 0;
}
}
}
'C# > Basics' 카테고리의 다른 글
W007. ThreeButtons (0) | 2021.04.25 |
---|---|
W006.HelloWorld (0) | 2021.04.25 |
Class. 속성 (0) | 2021.04.25 |
Class. 생성자 메소드 (0) | 2021.04.25 |
Class.인스턴스 메소드와 스태틱 메소드 (0) | 2021.04.25 |