0

我一直在开发 Trivia Game 应用程序以深入研究独立学习,最近将我的 Trivia Game 从控制台应用程序转移到 Windows 窗体应用程序。我现在遇到了麻烦,因为我在让我的 Windows 应用程序做我想做的事情时遇到了问题。

到目前为止我的程序的基本功能:

  1. 我有一个标签,我想从我的数组中一次显示一个问题。

  2. 我有一个用户输入答案的文本框,我希望将该文本框与答案数组进行比较,并确定用户是正确还是不正确。

我能够显示第一个问题,并且用户的答案确定正确/不正确工作正常,尽管在第二个问题显示后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;
   }
}

有没有办法在显示第二个问题后添加中断或暂停,以便我的代码等待用户输入并回答,然后再确定它是正确还是不正确?

4

2 回答 2

2

您可以通过整数变量跟踪实际问题。然后,您只需将答案按钮 clicl 侦听器重写为:

int count = 0;
private void AnalyzeQuestion(int x)
{

    if (txtanswer.Text == entertainmentanswers[x])
    {
        MessageBox.Show("You got this one right!", "Correct!");
        correct += 1;
    }
    else
    {
        MessageBox.Show("You got this one wrong! The correct answer was " + entertainmentanswers[x], "Wrong!");
        incorrect += 1;
    }
}
private void btnanswer_Click(object sender, EventArgs e)
{
    //button pressed to submit answer
    AnalyzeQuestion(count);
    count++;
    KeepScore();
    ResetPrompt();
    lblquestion.Text = entertainmentquestions[count];
}
于 2013-11-07T22:24:58.897 回答
1

您的问题之一是像 Coneone 已经指出的索引和字符串检查(相对于大写或小写)。您可以尝试这种设计(当然,完美还有很长的路要走,可能需要改进),但大致做到了作业,您可以将代码粘贴到新的 WindowsForms 项目并尝试一下:

1)创建一个新项目后,添加一个新类并将其命名为 Quiz 并将其粘贴到那里:

    public class Quiz
    {
        public string Question { get; set; }
        public string Answer { get; set; }
        private bool isAnswered = false;
        public bool IsAnswered
        {
            get { return isAnswered; }
            set { isAnswered = value; }
        }

        public Quiz(string question,string answer)
        {
            Question = question;
            Answer = answer;
        }
    }

2)然后在您的表单中粘贴此代码(当然您必须添加按钮、标签和文本框并适当地命名它们并将事件设置为它们各自的处理程序):

    public partial class Form1 : Form
    {
        Dictionary<int, Quiz> questions;
        Random rand = new Random();
        int position = 0;
        int correct = 0;
        int incorrect = 0;

        public Form1()
        {
            InitializeComponent();
            btnanswer.Enabled = false;
            questions = new Dictionary<int, Quiz>()
            {
                {0,new Quiz("What year did President Eisenhower become relieved of Presidency?","1982")},
                {1,new Quiz("What U.S. base was bombed forcing the United States to become involved in World War II","PEARL HARBOR")},
                {2,new Quiz( "What was the profession of Abraham Lincolns' assassin?","ACTOR")},
            };
        }


        private void Form1_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.Enabled = false;
        }

        private void KeepScore()
        {
            lblcorrect.Text = "Correct: " + correct;
            lblincorrect.Text = "Incorrect: " + incorrect;
        }

        private void txtanswer_TextChanged(object sender, EventArgs e)
        {
            //textbox always return empty string but i placed this here
            //so no need to create a variable and let you know
            //about other options....
            if (!string.IsNullOrEmpty(txtanswer.Text))
            {
                btnanswer.Enabled = true;
            }
            else
            {
                btnanswer.Enabled = false;
            }
        }

        private void ResetPrompt()
        {
            lblquestion.Text = "";
            txtanswer.Text = "";
        }

        private void AnalyzeQuestion()
        {
            if (string.Equals(txtanswer.Text, questions[position].Answer, StringComparison.CurrentCultureIgnoreCase))
            {
                MessageBox.Show("You got this one right!", "Correct!");
                correct += 1;
            }
            else
            {
                MessageBox.Show("You got this one wrong! the correct answer was " + questions[position].Answer);
                incorrect += 1;
            }
        }

        private void btnanswer_Click(object sender, EventArgs e)
        {
            AnalyzeQuestion();
            KeepScore();
            ResetPrompt();

            if (questions.Values.All(b => b.IsAnswered == true))
            {
                ResetAll();
                return;
            }
            GetQuestion();
        }

        private void btnstart_Click(object sender, EventArgs e)
        {
            btnstart.Enabled = false;
            GetQuestion();
        }

        private void GetQuestion()
        {
            position = rand.Next(0, 3);
            if (questions[position].IsAnswered != true)
            {
                questions[position].IsAnswered = true;
                lblquestion.BackColor = Color.Red;
                lblquestion.Text = questions[position].Question;
                txtanswer.Enabled = true;
            }
            else
            {
                while (questions[position].IsAnswered == true)
                {
                    position = rand.Next(0, 3);
                }
                questions[position].IsAnswered = true;
                lblquestion.BackColor = Color.Red;
                lblquestion.Text = questions[position].Question;
                txtanswer.Enabled = true;
            }
        }

        private void ResetAll()
        {
            txtanswer.Enabled = false;
            btnanswer.Enabled = false;
            position = 0;
            foreach (var item in questions.Values)
            {
                item.IsAnswered = false;
            }
            lblquestion.Text = "Game Over!!!...Please Press Start to Play Again.";
            btnstart.Enabled = true;
        }
    }
于 2013-11-07T23:48:33.150 回答