我一直在开发 Trivia Game 应用程序以深入研究独立学习,最近将我的 Trivia Game 从控制台应用程序转移到 Windows 窗体应用程序。我现在遇到了麻烦,因为我在让我的 Windows 应用程序做我想做的事情时遇到了问题。
到目前为止我的程序的基本功能:
我有一个标签,我想从我的数组中一次显示一个问题。
我有一个用户输入答案的文本框,我希望将该文本框与答案数组进行比较,并确定用户是正确还是不正确。
我能够显示第一个问题,并且用户的答案确定正确/不正确工作正常,尽管在第二个问题显示后lblquestion
,它甚至在给出答案之前就确定答案不正确,我无法弄清楚。我曾尝试在 dotnetpearls.com 和其他网站上进行在线研究,并阅读数组并执行 while 循环,但似乎仍然无法找到一种方法来完成这项工作。
这是我迄今为止一直在使用的代码:
public partial class frmentertainment : Form
{
string[] entertainmentanswers = { "1982", "PEARL HARBOR","ACTOR" };
string[] entertainmentquestions = { "What year did President Eisenhower become relieved of Presidency?", "What U.S. base was bombed forcing the United States to become involved in World War II", "What was the profession of Abraham Lincolns' assassin?"};
int correct = 0;
int incorrect = 0;
public frmentertainment()
{
InitializeComponent();
btnanswer.Enabled = false;
}
private void frmentertainment_Load(object sender, EventArgs e)
{
lblquestion.Text = ("Welcome! In this category of Trivia you will be quizzed on questions about movies, actors/actresses, television shows and more! Press 'Start Trivia' when you are ready");
txtanswer.Visible = false;
}
//track correct and incorrect answers
private void KeepScore()
{
lblcorrect.Text = "Correct: " + correct;
lblincorrect.Text = "Incorrect: " + incorrect;
}
private string txtboxvalue = "";
private void txtanswer_TextChanged(object sender, EventArgs e)
{
//making sure txt is entered into txtbox
if (txtanswer.Text != txtboxvalue)
{
btnanswer.Enabled = true;
}
else
{
btnanswer.Enabled = false;
}
}
//not working yet
private void AskQuestions()
{
for (int i = 0; i < entertainmentquestions.Length; i++)
{
lblquestion.Text = entertainmentquestions[i];
}
}
private void ResetPrompt()
{
lblquestion.Text = "";
txtanswer.Text = "";
}
private void AnalyzeFirstQuestion()
{
//determine if answer is wrong/right
if (txtanswer.Text == entertainmentanswers[0])
{
MessageBox.Show("You got this one right!", "Correct!");
correct += 1;
}
else
{
MessageBox.Show("You got this one wrong! the correct answer was " + entertainmentanswers[0]);
incorrect += 1;
}
}
private void AnalyzeSecondQuestion()
{
if (txtanswer.Text == entertainmentanswers[1])
{
MessageBox.Show("You got this one right!", "Correct!");
correct += 1;
}
else
{
MessageBox.Show("You got this one wrong! The correct answer was " + entertainmentanswers[1], "Wrong!");
incorrect += 1;
}
}
private void btnanswer_Click(object sender, EventArgs e)
{
//button pressed to submit answer
AnalyzeFirstQuestion();
KeepScore();
ResetPrompt();
lblquestion.Text = entertainmentquestions[1];
AnalyzeSecondQuestion();
}
private void btnstart_Click(object sender, EventArgs e)
{
//begin trivia, clicking this begins the first question
btnstart.Visible = false;
lblquestion.Text = entertainmentquestions[0];
txtanswer.Visible = true;
}
}
有没有办法在显示第二个问题后添加中断或暂停,以便我的代码等待用户输入并回答,然后再确定它是正确还是不正确?