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문을 사용하면 안되는건가요?