0

私は、独立した学習を掘り下げるために雑学ゲーム アプリケーションに取り組んでおり、最近、雑学ゲームをコンソール アプリケーションから Windows フォーム アプリケーションに移行しました。Windows アプリケーションで自分のやりたいことを実行する際に問題が発生しているため、現在問題が発生しています。

これまでの私のプログラムの基本的な機能:

  1. 配列から質問を 1 つずつ表示するラベルがあります。

  2. ユーザーが回答を入力するテキストボックスがあり、そのテキストボックスを回答配列と比較して、ユーザーが正しいか間違っているかを判断したいと考えています。

最初の質問が表示され、ユーザーの回答が正しい/正しくないと判断されましたが、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;
   }
}

2 番目の質問を表示した後に休憩または一時停止を追加して、コードが正しいか間違っているかを判断する前に、ユーザーが入力して回答するのを待つ方法はありますか?

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

あなたの問題の 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 に答える