Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#显示字符串数组中的问题/将用户输入与第二个数组中的答案进行比较_C#_Arrays_Loops_User Input_Windows Forms Designer - Fatal编程技术网

C#显示字符串数组中的问题/将用户输入与第二个数组中的答案进行比较

C#显示字符串数组中的问题/将用户输入与第二个数组中的答案进行比较,c#,arrays,loops,user-input,windows-forms-designer,C#,Arrays,Loops,User Input,Windows Forms Designer,我一直在开发一个琐事游戏应用程序来深入研究独立学习,最近我将我的琐事游戏从控制台应用程序转移到了Windows窗体应用程序。我现在遇到了麻烦,因为我在让我的Windows应用程序做我想做的事情时遇到了问题 迄今为止我的程序的基本功能: 我有一个标签,我想从数组中一次显示一个问题 我有一个用户输入答案的文本框,我希望该文本框与答案数组进行比较,并确定用户是否正确 我能够显示第一个问题,并且用户的答案被确定为正确/不正确工作正常,尽管在lblquestion中显示第二个问题后,它甚至在给出答案之前就

我一直在开发一个琐事游戏应用程序来深入研究独立学习,最近我将我的琐事游戏从控制台应用程序转移到了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;
       }
    }
    
    公共部分类frmentertainment:表单
    {
    字符串[]娱乐回答={“1982”,“珍珠港”,“演员”};
    string[]娱乐问题={“艾森豪威尔总统在哪一年被解除总统职务?”,“美国哪个基地被轰炸,迫使美国卷入第二次世界大战”,“亚伯拉罕·林肯的刺客是什么职业?”;
    int correct=0;
    int不正确=0;
    公共娱乐
    {
    初始化组件();
    btnanswer.Enabled=false;
    }
    私有void frmentertainment\u加载(对象发送方,事件参数e)
    {
    lblquestion.Text=(“欢迎!在这类琐事中,您将被问及有关电影、演员、电视节目等的问题!准备好后按“开始琐事”);
    txtanswer.Visible=false;
    }
    //跟踪正确答案和错误答案
    私有void KeepScore()
    {
    lblcorrect.Text=“正确:”+正确;
    lblincorrect.Text=“不正确:”+不正确;
    }
    私有字符串txtboxvalue=“”;
    私有void txtanswer_TextChanged(对象发送方,事件参数e)
    {
    //确保txt已输入txtbox
    if(txtanswer.Text!=txtboxvalue)
    {
    btnanswer.Enabled=true;
    }
    其他的
    {
    btnanswer.Enabled=false;
    }
    }
    //还没开始工作
    私人问题
    {
    for(int i=0;i

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

    您可以通过一个整数变量跟踪实际问题。 然后您只需将应答按钮clicl listener重写为:

    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];
    }
    

    您可以通过一个整数变量跟踪实际问题。 然后您只需将应答按钮clicl listener重写为:

    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];
    }
    

    您可以通过一个整数变量跟踪实际问题。 然后您只需将应答按钮clicl listener重写为:

    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];
    }
    

    您可以通过一个整数变量跟踪实际问题。 然后您只需将应答按钮clicl listener重写为:

    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];
    }
    

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

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

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

    公共部分类表单1:表单
    {
    词典问题;
    Random rand=新的Random();
    int位置=0;
    int correct=0;
    int不正确=0;
    公共表格1()
    {
    初始化组件();
    btnanswer.Enabled=false;
    问题=新字典()
    {
    {0,新测验(“艾森豪威尔总统是哪一年?”