【问题标题】:When I select the incorrect answer the next question loads with the answer already displayed当我选择不正确的答案时,下一个问题会加载已显示的答案
【发布时间】:2019-11-29 13:22:00
【问题描述】:

我对编码和 c# 完全陌生。我正在学习 3 级 ICT,在我的编程科目中,我们被要求对学生的基本技能进行测验(使用表格。)。 我被要求创建一个非常基本的登录屏幕、主题选择屏幕、级别选择屏幕以及实际的问题和答案;答案是按钮。

我的问题是,我选择了下一个问题的错误答案,但显示了该问题的正确答案。

不太确定我需要做什么。我想在周末解决这个问题,所以我无法问我的老师。

必须保持非常基本。

这是我的代码。感谢您对此主题的任何帮助。

0级表格-

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;

namespace EssentialSkills.Numeracy
{
    public partial class Level0num : Form
    {
        Point p1 = new Point(142, 303);
        Point p2 = new Point(322, 303);
        Point p3 = new Point(499, 303);
        List<Point> points = new List<Point>();


        public Level0num()
        {
            InitializeComponent();

            Globals.listQuestionsNum0.Add("1 + 1");
            Globals.listQuestionsNum0.Add("9 x 1");
            Globals.listQuestionsNum0.Add("10 x 3");
            Globals.listQuestionsNum0.Add("3 / 3");
            Globals.listQuestionsNum0.Add("2 + 5 + 6");
            Globals.listQuestionsNum0.Add("8 x 3");
            Globals.listQuestionsNum0.Add("99 - 87");
            Globals.listQuestionsNum0.Add("60 + 10 ");
            Globals.listQuestionsNum0.Add("55 + 15");
            Globals.listQuestionsNum0.Add("40 x 20");
            Globals.listQuestionsNum0.Add("120 / 60");
            Globals.listQuestionsNum0.Add("1.0 + 1.2");
            Globals.listQuestionsNum0.Add("3 x 13");
            Globals.listQuestionsNum0.Add("2 x 2");
            Globals.listQuestionsNum0.Add("10 x 30");
            Globals.listQuestionsNum0.Add("8 x 9");
            Globals.listQuestionsNum0.Add("9 x 9");
            Globals.listQuestionsNum0.Add("3 x 1");
            Globals.listQuestionsNum0.Add("4 / 4");
            Globals.listQuestionsNum0.Add("21 / 7");
            Globals.listQuestionsNum0.Add("42 / 7");
            Globals.listQuestionsNum0.Add("10 + 10 - 1");
            Globals.listQuestionsNum0.Add("84 - 2 + 3");
            Globals.listQuestionsNum0.Add("50 + 9 + 8");
            Globals.listQuestionsNum0.Add("-1 + 2");
            Globals.listQuestionsNum0.Add("33 + 88");
            Globals.listQuestionsNum0.Add("10 - 3");
            Globals.listQuestionsNum0.Add("32 + 2 x 1");
            Globals.listQuestionsNum0.Add("1 x 543 + 1");
            Globals.listQuestionsNum0.Add("3 + 10 x 2");

            Globals.listAnswersNum0.Add(1 + 1);
            Globals.listAnswersNum0.Add(9 * 1);
            Globals.listAnswersNum0.Add(10 * 3);
            Globals.listAnswersNum0.Add(3 / 3);
            Globals.listAnswersNum0.Add(2 + 5 + 6);
            Globals.listAnswersNum0.Add(8 * 3);
            Globals.listAnswersNum0.Add(99 - 87);
            Globals.listAnswersNum0.Add(60 + 10);
            Globals.listAnswersNum0.Add(55 + 15);
            Globals.listAnswersNum0.Add(40 * 20);
            Globals.listAnswersNum0.Add(120 / 60);
            Globals.listAnswersNum0.Add(1.0 + 1.2);
            Globals.listAnswersNum0.Add(3 * 13);
            Globals.listAnswersNum0.Add(2 * 2);
            Globals.listAnswersNum0.Add(10 * 30);
            Globals.listAnswersNum0.Add(8 * 9);
            Globals.listAnswersNum0.Add(9 * 9);
            Globals.listAnswersNum0.Add(3 * 1);
            Globals.listAnswersNum0.Add(4 / 4);
            Globals.listAnswersNum0.Add(21 / 7);
            Globals.listAnswersNum0.Add(42 / 7);
            Globals.listAnswersNum0.Add(10 + 10 - 1);
            Globals.listAnswersNum0.Add(84 - 2 + 3);
            Globals.listAnswersNum0.Add(50 + 9 + 8);
            Globals.listAnswersNum0.Add(-1 + 2);
            Globals.listAnswersNum0.Add(33 + 88);
            Globals.listAnswersNum0.Add(10 - 3);
            Globals.listAnswersNum0.Add(32 + 2 * 1);
            Globals.listAnswersNum0.Add(1 * 543 * 1);
            Globals.listAnswersNum0.Add(3 + 10 * 2);

            points.Add(p1);
            points.Add(p2);
            points.Add(p3);


        }
        private void LoadQuestions()
        {
            lblCountdownval.Visible = false;
            lblCountdown.Visible = false;


            Globals.intQuestionNumber += 1;
            lblQuestionsNumber.Text = "Question Number: " + Globals.intQuestionNumber.ToString();


            Random random = new Random();
            Globals.listIndex = random.Next(0, Globals.listAnswersNum0.Count - 1);
            lblQuestion.Text = Globals.listQuestionsNum0.ElementAt(Globals.listIndex);
            btnCorrect.Text = Globals.listAnswersNum0.ElementAt(Globals.listIndex).ToString();
            btnAnswer1.Text = random.Next(100).ToString();
            btnAnswer3.Text = random.Next(100).ToString();

            int locationIndex = random.Next(0, 3);
            btnCorrect.Location = points.ElementAt(locationIndex);

            locationIndex = random.Next(0, 3);

            btnAnswer1.Location = points.ElementAt(locationIndex);
            while ((btnAnswer1.Location == btnCorrect.Location))
            {
                locationIndex = random.Next(0, 3);
                btnAnswer1.Location = points.ElementAt(locationIndex);

            }
            locationIndex = random.Next(0, 3);
            btnAnswer3.Location = points.ElementAt(locationIndex);

            while ((btnAnswer3.Location == btnCorrect.Location) || (btnAnswer3.Location == btnAnswer1.Location))
            {
                locationIndex = random.Next(0, 3);
                btnAnswer3.Location = points.ElementAt(locationIndex);
            }
        }

        public void showCorrectAnswer()
        { 
            timerLoadQuestion.Start();
            lblCountdownval.Visible = true;
            lblCountdown.Visible = true;
            lblCountdownval.Text = "5";
            lblCountdown.Text = "Next question will load in .... ";

            btnAnswer1.Visible = false;
            btnAnswer3.Visible = false;
            btnCorrect.Location = p2;
            btnCorrect.BackColor = Color.Green;

        }

        private void Level0num_Load(object sender, EventArgs e)
        {
            lblLoggedUser.Text = Globals.loggedUser;

            lblQuestionsNumber.Text = "Question Number: ";
            LoadQuestions();

            Globals.s = 0;
            lblScore.Text = "Score: " + Globals.s;

        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            frmNumeracy back = new frmNumeracy();
            this.Close();
            back.ShowDialog();
        }

        private void btnAnswer1_Click(object sender, EventArgs e)
        {

            showCorrectAnswer();
            MessageBox.Show("Incorrect");
            LoadQuestions();

        }

        private void btnAnswer3_Click(object sender, EventArgs e)
        {
            showCorrectAnswer();
            MessageBox.Show("Incorrect");
            LoadQuestions();

        }

        private void btnCorrect_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Correct!");

            Globals.s += 1;
            lblScore.Text = "Score: " + Globals.s.ToString();
            LoadQuestions();
        }
    }
}

这是我的全局类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EssentialSkills
{
    public static class Globals
    {
        public static string username, password;

        public static string user1, user2, user3, user4, user5, user6, loggedUser, admin;
        public static string pass1, pass2, pass3, pass4, pass5, pass6, adminPass1;

        public static List<string> listQuestionsNum0 = new List<string>();
        public static List<double> listAnswersNum0 = new List<double>();

        public static int intQuestionNumber = 0;

        public static int listIndex;

        public static int s;
    }
}

【问题讨论】:

    标签: c# winforms linq


    【解决方案1】:

    很难准确地说出,因为您粘贴的代码不足以在我这边复制它,但是从快速扫描来看,您似乎依赖于很多状态(例如按钮位置/位置、颜色,可见性等)保持在主窗体上的每个控件中。由于没有为每个问题重新创建表单,因此很可能在您对第一个问题的初始化中存在一些假设,而该假设没有为第二个问题(以及后续问题)明确设置。

    因此,例如,如果您将 btnCorrect.BackColor 设置为绿色以获得成功的答案,请确保为下一个问题(例如在 LoadQuestions 中)将其重新初始化为红色(或表单中的任何默认值) ())。

    【讨论】:

    • 感谢您的回答。我可以帮助您复制此代码,并且您告诉我在 loadQuestions() 结束时将按钮的颜色值设置回白色(默认)吗?
    • 另外我应该有一个计时器,它可以倒计时问题在屏幕上停留多长时间,然后移动到下一个问题。一个似乎不起作用的功能。
    猜你喜欢
    • 2018-11-30
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多