2021年3月6日星期六

How do I loop through user input and tell them if they are correct or incorrect?

I have a working program that the user enters their 10 answers into the 10 textboxes. They are then checked and a message is displayed how many they got right out of 10.

The problem: I am trying to have the corresponding label have a background color of green and display Correct. Or if it was incorrect have the label background color turn red and display Incorrect, the correct answer is: (Then display the correct answer).

Code won't compile, what am I doing wrong. The problem is in the submit button.

Code:

using System;  using System.Collections.Generic;  using System.ComponentModel;  using System.Data;  using System.Drawing;  using System.Linq;  using System.Text;  using System.Threading.Tasks;  using System.Windows.Forms;  using System.IO;    namespace MathTestwithListsFreed  {      public partial class Form1 : Form      {          public Form1()          {              InitializeComponent();          }          List<int> answernumbers = new List<int>();          List<TextBox> listoftextboxes = new List<TextBox>();          List<Label> listoflabels = new List<Label>();          private void ExitButton_Click(object sender, EventArgs e)          {              Close();          }            private void Form1_Load(object sender, EventArgs e)          {              listoftextboxes.Add(textBox01);              listoftextboxes.Add(textBox02);              listoftextboxes.Add(textBox03);              listoftextboxes.Add(textBox04);              listoftextboxes.Add(textBox05);              listoftextboxes.Add(textBox06);              listoftextboxes.Add(textBox07);              listoftextboxes.Add(textBox08);              listoftextboxes.Add(textBox09);              listoftextboxes.Add(textBox10);              listoflabels.Add(Answerlabel01);              listoflabels.Add(Answerlabel02);              listoflabels.Add(Answerlabel03);              listoflabels.Add(Answerlabel04);              listoflabels.Add(Answerlabel05);              listoflabels.Add(Answerlabel06);              listoflabels.Add(Answerlabel07);              listoflabels.Add(Answerlabel08);              listoflabels.Add(Answerlabel09);              listoflabels.Add(Answerlabel10);                try              {                  StreamReader answerReader = File.OpenText("answers.txt");                  while (!answerReader.EndOfStream)                  {                      answernumbers.Add(int.Parse((answerReader.ReadLine())));                  }                  answerReader.Close();              }              catch (Exception ex)              {                  MessageBox.Show(ex.Message);              }          }            private void ClearButton_Click(object sender, EventArgs e)          {              int x = 0;              listoflabels.Clear();              listoftextboxes.ForEach(txt => txt.Clear());              listoflabels[x].BackColor = DefaultBackColor;            }            private void SubmitButton_Click(object sender, EventArgs e)          {              int correct = 0;              int i = 0;                            for (int x = 0; x < listoftextboxes.Count; x++)              {                  if (answernumbers[x] == Convert.ToInt32(listoftextboxes[x].Text))                  {                      correct++;                      if (answernumbers[i] != Convert.ToInt32(listoflabels[i].Text));                      {                          listoflabels[x].BackColor = Color.Red;                          listoflabels = ("Incorrect, the correct answer is: " + correct.ToString());                       }                      else                      {                          listoflabels[x].BackColor = Color.Green;                          listoflabels = ("Correct".ToString());                      }                  }                }                            MessageBox.Show("You answered " + correct.ToString() + " out of 10 correctly");          }      }  }  

Should look something like:

enter image description here

https://stackoverflow.com/questions/66512462/how-do-i-loop-through-user-input-and-tell-them-if-they-are-correct-or-incorrect March 07, 2021 at 09:45AM

没有评论:

发表评论