비주얼 프로그래밍

비주얼 프로그래밍 3주차 <성적 계산기 만들기>

23615038 이서경 2024. 3. 25. 23:42
더보기

# 라디오 버튼과 그룹박스

* 라디오 버튼 -> 그룹박스와 함께 사용 가능 BUT 그룹박스 안의 라디오버튼 하나만 체크 가능

private void button1_Click(object sender, EventArgs e)
        {
            string result = "국적 : ";
            if (rb_korea.Checked)
                result += "대한민국\n";
            else if (rd_china.Checked)
                result += "중국\n";
            else if (rb_japan.Checked)
                result += "일본\n";
            else if (rb_others.Checked)
                result += "그 외의 국가 \n";

            if (rb_male.Checked)
                result += "성별 : 남성 ";
            else if (rb_female.Checked)
                result += "성별 : 여성";

            MessageBox.Show(result, "결과");
        }

 

코드 출력

 

 

더보기

# 성적 계산기

* 점수 입력, 계산 결과 출력 = TextBOX사용

* 출력에 사용하는 TextBOX는 Readonly 속성으로 변경

* TextBOX 중앙에 TextAlignment속성 > Center로 설정

★ To string : 문자열을 숫자로 변환

★ ToDouble : string를 Double로 변

public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    double sum = Convert.ToDouble(textBox1.Text)
        +Convert.ToDouble(textBox2.Text)
        + Convert.ToDouble(textBox3.Text);

    double avg = sum / 3;
    textBox4.Text = sum.ToString();
    textBox5.Text = avg.ToString("0.0"); // 소수점 한자리
}
성적 입력 후 출력 화면

 

더보기

# 학점 계산기

public partial class Form1 : Form
    {
        TextBox[] titles; // 교과목 textbox 배열
        ComboBox[] crds; // 학점 combobox 배열
        ComboBox[] grds; // 성적 combobox 배열
        public Form1() // 생성자함수
        {
            InitializeComponent();
        }
        private void bt_avg_Click(object sender, EventArgs e)
        {
            double totalScore = 0;
            int totalCredits = 0;

            for (int i =0; i < titles.Length; i++)
            {
                if (titles[i].Text !="")
                {
                    int crd = int.Parse(crds[i].SelectedItem.ToString()); 
                    totalCredits += crd;
                    totalScore += crd * GetGrade(grds[i].SelectedItem.ToString()) ;
                }
            }

            txt_grade.Text = (totalScore / totalCredits).ToString("0.0");
        }

        private double GetGrade(string v) // 
        {
            double grade = 0;

            if (v == "A+") grade = 4.5;
            else if (v == "A0") grade = 4.0;
            else if (v == "B+") grade = 3.5;
            else if (v == "B0") grade = 3.0;
            else if (v == "C+") grade = 2.5;
            else if (v == "C0") grade = 2.0;
            else if (v == "D+") grade = 1.5;
            else if (v == "D0") grade = 1.0;
            else grade = 0;

            return grade;
        }

        private void Form1_Load(object sender, EventArgs e) // 프로그램 시작
        {
            txt_1.Text = "인체의 구조와 기능";
            txt_2.Text = "일반수학";
            txt_3.Text = "전기전자회로공학";
            txt_4.Text = "데이터사이언스 ";
            txt_5.Text = "설계및프로젝트";
            txt_6.Text = "영어";
            txt_7.Text = "영세여";
            txt_8.Text = "비주얼 프로그래밍";

            //학점 콤보박스의 배열
            crds = new ComboBox[] { crd_1, crd_2, crd_3, crd_4, crd_5, crd_6, crd_7, crd_8 };
            //성적 콤보박스의 배열
            grds = new ComboBox[] {grd_1, grd_2, grd_3 , grd_4 , grd_5, grd_6, grd_7, grd_8 };
            //교과목 텍스트 박스 배열 
            titles = new TextBox[] {txt_1, txt_2, txt_3, txt_4, txt_5, txt_6, txt_7, txt_8};
            //학점을 콤보박스에 표시하기 위해 배열을 만듦.
            int[] arr = { 1, 2, 3, 4, 5 };
            //성적을 콤보박스에 표시하기 위해 리스트 만듦
            List<string> list = new List<string> { "A+", "A0", "B+", "B0", "C+", "C0", "D+", "D0", "F" };
            //학점 콤보박스
            foreach (var c in crds)
            {
                foreach(var i in arr)
                    c.Items.Add(i);
                c.SelectedItem = 3;
            }
            //성적 콤보박스
            foreach (var c in grds)
            {
                foreach (var s in list)
                    c.Items.Add(s);
                c.SelectedItem = "+A";
            }

        }
    }
}

 

  • form1_road 이벤트 함수 공부하기
  • crds =new combobox[] {~~~~} // 초기화 시키다
  • form1_load : 폼을 시작 하기 전에 ...
출력 화면