用visual studio设计一个多选题
时间: 2024-04-01 11:24:57 浏览: 14
以下是一个示例程序,演示如何在Visual Studio中设计一个多选题:
```csharp
using System;
using System.Windows.Forms;
namespace MultiChoiceQuestion
{
public partial class Form1 : Form
{
string[] questions = new string[]
{
"What is the capital of France?",
"Who invented the telephone?",
"What is the largest planet in our solar system?"
};
string[][] choices = new string[][]
{
new string[] {"Paris", "London", "Berlin", "Rome"},
new string[] {"Alexander Graham Bell", "Thomas Edison", "Nikola Tesla", "Benjamin Franklin"},
new string[] {"Jupiter", "Saturn", "Neptune", "Uranus"}
};
int[] answers = new int[] { 0, 0, 0 };
int currentQuestion = 0;
int score = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DisplayQuestion();
}
private void DisplayQuestion()
{
lblQuestion.Text = questions[currentQuestion];
cbChoice1.Text = choices[currentQuestion][0];
cbChoice2.Text = choices[currentQuestion][1];
cbChoice3.Text = choices[currentQuestion][2];
cbChoice4.Text = choices[currentQuestion][3];
cbChoice1.Checked = false;
cbChoice2.Checked = false;
cbChoice3.Checked = false;
cbChoice4.Checked = false;
}
private void btnNext_Click(object sender, EventArgs e)
{
int selectedChoices = 0;
if (cbChoice1.Checked)
selectedChoices++;
if (cbChoice2.Checked)
selectedChoices++;
if (cbChoice3.Checked)
selectedChoices++;
if (cbChoice4.Checked)
selectedChoices++;
if (selectedChoices == 0)
{
MessageBox.Show("Please select at least one choice.");
return;
}
int[] selectedAnswers = new int[selectedChoices];
int index = 0;
if (cbChoice1.Checked)
{
selectedAnswers[index] = 0;
index++;
}
if (cbChoice2.Checked)
{
selectedAnswers[index] = 1;
index++;
}
if (cbChoice3.Checked)
{
selectedAnswers[index] = 2;
index++;
}
if (cbChoice4.Checked)
{
selectedAnswers[index] = 3;
index++;
}
bool isCorrect = true;
foreach (int answer in selectedAnswers)
{
if (answers[currentQuestion] != answer)
{
isCorrect = false;
break;
}
}
if (isCorrect)
score++;
currentQuestion++;
if (currentQuestion < questions.Length)
{
DisplayQuestion();
}
else
{
MessageBox.Show("Your score is " + score + " out of " + questions.Length + ".");
Application.Exit();
}
}
}
}
```
这个程序使用了Windows窗体,包含一个标签、4个复选框和一个按钮。在窗体加载时,它会显示第一个问题。当用户点击“下一步”按钮时,程序会检查用户选择的复选框是否至少选择了一个选项,并且会根据用户的选择判断答案是否正确。然后它会显示下一个问题或者在所有问题都回答完毕后显示得分。