AD

Q&A C# 질문

xxxsmith22
2019-11-26 15:08:23 1119 0 1

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 ch_12_객체동적생성

{

    public partial class 랜덤시험문제 : Form

    {

        GroupBox[] GB = new GroupBox[5]; // 그룹 박스 

        RadioButton[,] Bogi = new RadioButton[5, 5];  // 보기 

        Label[] Quiz = new Label[5];            //문제

        Label lbScore = new Label();     // 점수 표시 Label  

        Label lb1 = new Label();  //중복 제거 전 라벨

        Label lb2 = new Label(); //중복 제거 후 라벨

        Button button = new Button();  // Button 

        int Score = 0;                          // 점수 변수


        public 랜덤시험문제()

        {

            InitializeComponent();

            string aa = "[문제 ";   

            string[,] Question = new string[5, 6] {

            {"인천에 있는 산은 ?", "한라산", "백두산", "계양산", "금강산", "계양산"},

            { "다음중 1+1 = ?" ,"일", "이", "삼","사", "이"},

            { "다음중 1+2 = ?" ,"일", "이", "삼","사", "삼"},

            { "다음중 1+3 = ?" ,"일", "이", "삼","사", "사"},

            { "다음중 1+4 = ?" ,"오", "육", "칠","팔", "오"}

            };

            #region //점수표시할Label생성

            lbScore = new Label();

            this.lbScore.Name = "lbScore";

            this.lbScore.Text = "Score : ";

            this.lbScore.Size = new Size(90, 30);

            this.lbScore.AutoSize = true;

            this.lbScore.Location = new System.Drawing.Point(30, 470);

            this.Controls.Add(lbScore);


            lb1 = new Label();

            this.lb1.Name = "lb1";

            this.lb1.Text = "중복제거 전 : ";

            this.lb1.Size = new Size(90, 30);

            this.lb1.AutoSize = true;

            this.lb1.Location = new System.Drawing.Point(400, 300);

            this.Controls.Add(lb1);


            lb2 = new Label();

            this.lb2.Name = "lb2";

            this.lb2.Text = "중복제거 후 : ";

            this.lb2.Size = new Size(90, 30);

            this.lb2.AutoSize = true;

            this.lb2.Location = new System.Drawing.Point(400, 350);

            this.Controls.Add(lb2);

            #endregion

            

            #region //버튼생성

            this.button.Name = "btnSubmit";

            this.button.Text = "채점하기";

            this.button.Size = new Size(90, 30);

            this.button.AutoSize = true;

            this.button.Location = new System.Drawing.Point(180, 470);

            this.button.Click += new EventHandler(button_Click);

            this.Controls.Add(button);

            #endregion

            #region // 랜덤값 선택한 것을 저장할 배열 문제 랜덤 선택, 보기 랜덤 선택

            Random num = new Random();

            int[] mun = new int[10];//중복 제거전 문제

            for (int i = 0; i < 10; i++)

            {

                mun[i] = (int)num.Next(0, 5);

                this.lb1.Text += Convert.ToString(mun[i]);

                lb1.Text += " ";

            }

            mun = mun.Distinct().ToArray();//중복 제거 후 문제

            for (int i = 0; i < 3; i++)

            {

                lb2.Text += Convert.ToString(mun[i]);

                lb2.Text += " ";

            }

            int last = 0;

            int[] arr = new int[5];

            arr = RandomBogi();

            for (int i = 0; i < 3; i++)

            {

                for (int k = 0; k < 5; k++)

                {

                    last = 0;

                    if (mun[i] == k)

                    {

                        GB[k] = new GroupBox();

                        this.GB[k].AutoSize = true;

                        this.GB[k].Location = new System.Drawing.Point(10, 10 + (i * 150));

                        this.GB[k].Size = new System.Drawing.Size(350, 120);

                        this.GB[k].TabIndex = 0;

                        this.GB[k].TabStop = false;

                        this.GB[k].Text = aa + (k + 1) + "]" + Question[k, 0];

                        this.Controls.Add(GB[k]);


                        foreach (int c in arr)

                        {                            

                            Bogi[k, c] = new RadioButton();

                            this.Bogi[k, c].Location = new System.Drawing.Point(30, 30 + (last * 20));

                            this.Bogi[k, c].Size = new System.Drawing.Size(100, 20);

                            this.Bogi[k, c].Text = Question[k, c + 1];

                            if (c == 4)

                            {

                                if (Bogi[k, 4] != null)

                                {

                                    Bogi[k, 4].Visible = false;

                                }

                                last--;

                            }

                            this.Controls.Add(Bogi[k, c]);

                            this.GB[k].Controls.Add(this.Bogi[k, c]);

                            last++;

                        }

                    }

                }

            }

            #endregion

            #region //채점하기

            void button_Click(object sender, EventArgs e)

            {

                Button button = sender as Button;

                Score = 0;

                for(int i = 0; i<3; i++)

                {

                    for(int j = 0; j<4; j++)

                    {

                        if ((Bogi[i, 4].Text == Bogi[i, j].Text) || (Bogi[i, 4].Text != null && Bogi[i, j].Text != null))

                            if (Bogi[i, j].Checked)

                                Score++;                           

                    }

                }

                lbScore.Text = "Score : " + Convert.ToString(Score);

            }

            #endregion

        }

        private static int[] RandomBogi()

        {

            int[] num = new int[5];

            Random randNum = new Random();

            for (int i = 0; i < 5; i++)

            {

                num[i] = (int)randNum.Next(0, 5);

                for (int j = 0; j < i; j++)

                {

                    if (num[i] == num[j])

                    {

                        i--;

                        break;

                    }

                }

            }

            return num;

        }

    }

}



 if ((Bogi[i, 4].Text == Bogi[i, j].Text) || (Bogi[i, 4].Text != null && Bogi[i, j].Text != null))

여기서 널예외가 발생하는데 foreach문을 사용하면 안되는건가요?

후원댓글 1
댓글 1개  
이전 댓글 더 보기
TWIP 잔액: 확인중
자유강의Q&A프로젝트 소개
3
프로젝트 소개
스트리밍 플랫폼 제작 및 개발 일기
도담도담한도담
06-25
1
05-09
0
05-10
3
04-26
0
자유
트게더에
목좋스만보는_토피니에요
03-10
0
Q&A
수학 [3]
ㅇㅇ
10-16
0
06-20
1
프로젝트 소개
[Go][RPi] 트위치 팔로워 카운터 [1]
suapapa
06-19
1
05-11
2
자유
트게더 크롤링하면 [3]
zoodasu
05-05
0
자유
C# 질문(해결 완료) [6]
딸랑짤랑
03-27
1
자유
코딩테스트 잘 보는 법 좀 ㅠㅠ [1]
아놔놔놔놔놔놔
12-12
»
Q&A
C# 질문 [1]
xxxsmith22
11-26
0
11-22
인기글 글 쓰기