目前我正在使用Windows窗体应用程序进行测验游戏。 所以我很困惑的是如何将我从Trivia.cs读取的数据(从ListOfData方法)分配给TriviaForm中的变量(questionText)和(答案),以便将它传递给Question.cs处理数据并找出哪一个是正确的答案。
我只是一名初学者,这是我的高中入门课程,所以我非常感谢你的帮助。
所以我做了一个Trivia课程(Trivia.cs)来阅读数据。
namespace D4
{
class Trivia
{
// Fields
private const string FileName = "WumpusTrivia.txt";
private string questionText;
private string[] answers = new string[4];
public Trivia()
{
}
/// This method will return the list of data.
public List<Question> ListOfData()
{
List<Question> ls = new List<Question>();
using (StreamReader sr = new StreamReader(FileName))
{
while (!sr.EndOfStream)
{
this.questionText = sr.ReadLine();
for (int i = 0; i < 4; i++)
this.answers[i] = sr.ReadLine();
sr.ReadLine();
ls.Add(new Question(this.questionText, this.answers));
}
}
return ls;
}
}
}
一个问题类(Question.cs),用于处理数据并返回值以放入文本框。
namespace D4
{
class Question
{
private string question;
private string[] answers = new string[4];
private int correctAnswer;
private string[] freeTrivia;
private Trivia trivia;
public Question(string question, string[] answers)
{
this.trivia = new Trivia();
this.question = question;
this.answers = answers;
for (int i = 0; i < 4; i++)
{
if (answers[i].ToString().EndsWith("+"))
{
this.correctAnswer = i;
answers[i].ToString().Replace('+', ' ');
}
}
}
public string QuestionText { get { return this.question; } }
public string[] Answers { get { return this.answers; } }
public int CorrectAnswer { get { return this.correctAnswer; } }
public string[] Freetrivia { get { return this.freeTrivia; } }
}
}
还有一个TriviaForm类(TriviaForm.cs)来显示文本框中Trivia类的数据。
namespace D4
{
public partial class TriviaForm : Form
{
//// Fields
private Trivia trivia;
private Question question;
private string questionText;
private string[] answers = new string[4];
private Question[] data;
private const int wumpusMaxQuestions = 5;
private const int hazardAndPurchaseMaxQuestions = 3;
// Replace these later after finished connecting all objects.
private bool hazardAndPurchase = false;
private bool wumpus = false;
public TriviaForm()
{
InitializeComponent();
this.trivia = new Trivia();
this.data = trivia.ListOfData().ToArray();
this.questionText = data[0].ToString();
for (int i = 0; i < 4; i++)
this.answers[i] = data[i].ToString();
this.question = new Question(questionText, answers);
QuestionOutputter();
}
// Displays the questions and answers on the form.
private void QuestionOutputter()
{
this.txtQuestion.Text = question.QuestionText;
this.txtBoxA.Text = question.Answers[0];
this.txtBoxB.Text = question.Answers[1];
this.txtBoxC.Text = question.Answers[2];
this.txtBoxD.Text = question.Answers[3];
}
}
}
对不起那些糟糕的问题,我很抱歉,但我终于明白了。我遇到的问题是问题类给出了错误。这是因为我使用questionText将null传递给它,并在TriviaForm类中回答变量。 我的代码:
this.data = trivia.ListOfData().ToArray();
this.questionText = data[0].ToString();
for (int i = 0; i < 4; i++)
this.answers[i] = data[i].ToString();
完全错了。显然我只需要做一个foreach循环。 我的解决方案:
this.data = trivia.ListOfData().ToArray();
foreach (Question q in this.data)
{
this.questionText = q.QuestionText.ToString();
for (int i = 0; i < 4; i++)
this.answers[i] = q.Answers[i];
this.question = new Question(questionText, answers);
}
我尝试了这个,但效果确实很好!抱歉浪费你的时间。
答案 0 :(得分:2)
在每个答案文本框旁边添加一个单选按钮,并将事件处理程序添加到这些单选按钮,以评估所选答案是否正确。
这样的事情可能适用于你现有的代码,假设你把ttBoxB旁边的radioButton2和txtBoxC旁边的radioButton3等等:
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (((System.Windows.Forms.RadioButton)sender).Text.Substring(11,1) = this.Question.correctAnswer + 1) {
CorrectAnswer_DoSomethingHere();
} else {
Incorrect_DoSomethingElse();
}
}