游戏中的Q / A级别没有找到正确的答案

时间:2017-12-07 14:54:52

标签: c#

我正在尝试在C#中为我的课程作出问题/答案。这是代码:

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;
using System.Linq;

namespace frmSplashScreen
{
    public partial class frmLevel3 : Form
    {
        public frmLevel3()
        {
            InitializeComponent();
        }
        string currentq = null;
        int questionsshown = 1; //to go up to 4
        int questionno = 0;

        public void randomq() //new question function... generating number, using it as index, putting it on lblQ
        {
            Random ran = new Random();
            questionno = ran.Next(20);

            label1.Text = questionno.ToString(); //debug
            label2.Text = gameClass.answers[questionno];//debug

            lblQ.Text = gameClass.questions[questionno];
            gameClass.questions.RemoveAt(questionno);
        }

        private void frmLevel3_Load(object sender, EventArgs e) // Load //
        {
            btnNext.Hide();
            using (StreamReader sr = new StreamReader("l3questions.txt")) //reading all questions from text file
            {
                string line;
                while ((line = sr.ReadLine()) != null) 
                {
                    gameClass.questions.Add(line);
                }

            }
            using (StreamReader sr = new StreamReader("l3answers.txt")) //reading all answers from text file
            {
                string line;
                while ((line = sr.ReadLine()) != null) 
                {
                    gameClass.answers.Add(line);
                }
            }
            randomq();
        }

        private void btnNext_Click(object sender, EventArgs e) // Next Button //
        {
            btnNext.Hide();
            btnCheck.Show();
            if (questionsshown >= 4) // Checking no. of questions shown
            {
                frmMainMenu menu = new frmMainMenu(); //Go to Menu
                this.Hide();
                menu.Show();
            }
            else
            {
                questionsshown++;
                randomq(); //if less than 4 questions have been shown, show another.
            }
            txt1a.BackColor = Color.White; //Setting txts back to normal
            txt1a.Text = null;
        } 
        private void btnCheck_Click(object sender, EventArgs e) // Check Button //
        {
            btnCheck.Hide();
            btnNext.Show();
            if (txt1a.Text.ToLower() == gameClass.answers[questionno]) //checking question
            {
                gameClass.score++;
                txt1a.BackColor = Color.Green;
            }
            else
            {
                txt1a.BackColor = Color.Red;
            }   
        }
    }
}

gameClass 是一个存储问题列表和答案列表的类。

我的问题是,当我去运行关卡时,它会在前2次从列表中找到正确的问题/答案,但在接下来的两次,它试图检查的答案是列表中完全随机的内容,而不是变量 questionno 中索引号应该采取的答案。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

在你的randomq函数中,你从文件中删除你的问题,但不是答案。单击按钮后,您将使用匹配的索引号检入答案测试文件。在第一个循环之后,您的文本文件将不同步,您的问题将少于答案,并且它们的索引将不匹配。

public void randomq() //new question function... generating number, using it as index, putting it on lblQ
    {
        Random ran = new Random();
        questionno = ran.Next(20);

        label1.Text = questionno.ToString(); //debug
        label2.Text = gameClass.answers[questionno];//debug

        lblQ.Text = gameClass.questions[questionno];
        //Add this
        someStringAnswer = gameClass.answers[questionno];
        gameClass.questions.RemoveAt(questionno);
        //And this
        gameClass.answers.RemoveAt(questionno);
    }

在此之后,使用someStringAnswer查看您是否在btnCheck_Click

中找到了正确的答案