我目前的情况:我正在以表格(Visual Studio)编写csharp测验。
这是一个多项选择测验,问题后跟3个选项,作为单选按钮。
我的问题:如何从单选按钮获得答案,然后如何转换所有答案以向用户提供正确的答案%?
我需要将答案和问题硬编码到程序中,并且不能使用数据库。我基本上是在我有限的知识(noob)的墙上。
答案 0 :(得分:0)
创建一个名为QuestionForm的表单,并创建用于设置标签文本或将标签设为公共的属性。在名为ShowQuestion的表单上创建一个函数,该函数将窗体显示为对话框并将所选答案作为整数返回。其余的是在您编辑之前基于您的问题
public partial class Form1 : Form
{
private struct Question
{
public Question(string q, string a1, string a2,string a3,int correct)
{
Q = q;
A1 = a1;
A2 = a2;
A3 = a3;
CorrectChoice = correct;
}
public string Q;
public string A1;
public string A2;
public string A3;
public int CorrectChoice;
}
Question[] questions;
int correctChoise = -1;
bool[] correctAnswers = new bool[10];
public Form1()
{
InitializeComponent();
questions = new Question[]
{ new Question("Linux is:", "An operating system", "A kernel", "A device", 0),
new Question("cisco are a global brand, who specialise in::", "Networking", "Gamingl" ,"Mobile applications", 0)
// Add more questions here
};
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void startbtn_Click(object sender, EventArgs e)
{
for (int i=0;i<10 ;i++ )
{
int answer = QuestionForm.ShowQuestion( questions[i].Q,
questions[i].A1,
questions[i].A2,
questions[i].A3);
correctAnswers[i] = answer == questions[i].CorrectChoise;
}
int correct = correctAnswers.Where(x=>x).Count();
MessageBox.Show(String.Format("Test Finished with {0}% correct", 100 * ((double)correct/10.0)));
}
}
问题表格:
public class QuestionForm
{
// skipped constructor and controls
public static int ShowQuestion(string q, string a1, string a2,string a3,int correct)
{
QuestionForm f = new QuestionForm();
f.qtnlabel.Text = q;
f.radbtnans1.Text = a1;
f.radbtnans2.Text = a2;
f.radbtnans3.Text = a3;
f.ShowDialog(owner);
if (f.radbtnans1.Checked)
return 0;
else if (f.radbtnans2.Checked)
return 1;
else if (f.radbtnans3.Checked)
return 2;
}
}
答案 1 :(得分:0)
仅尝试解决您提出的问题,此处的代码显示了如何从复选框中获取值并根据它获取总和和平均值:
List<int> values = new List<int>();
if (radioButton1.Checked) //correct answer to question 1 is box 1
values.Add(1);
else
values.Add(0);
if(radioButton4.Checked) //correct answer to question 2 is box 4
values.Add(1);
else
values.Add(0);
int sum = values.Sum();
int count = values.Count();
double score = sum / count * 100; //percentage score
再次,只是简单地回答你关于WinForms中的单选按钮的问题并获得测验分数。