C# 尝试将文件读取到数组并根据整数测试随机 select 一行

C# Trying to read a file to array and randomly select a line based on test of an integer

该解决方案旨在读取包含由三行组成的记录的文本文件。记录的第一行是通过测试值,第二行是问题,第三行是答案。

执行时,它会检查是否只有一条记录,或者是否有一条记录。

问题是当有多个时:
意思是在记录数量范围内随机select一个数字来决定记录什么。一旦确定了该记录,程序就会查看它生成的随机值是否在通过测试值的范围内与通过测试值匹配,以便批准该记录。所有这一切都是为了 运行 在一个 while 循环中,直到一个记录被 selected,并且随机值作为该记录的通过测试值通过。

我相信它应该可以工作,但是当我启动程序时它似乎处于无限循环中,因为当只有两条记录时或者当传递值为两个时。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Quiz : Form
    {
        Random rnd = new Random(0);
        public Quiz()
        {
            InitializeComponent();
            checkFile();
            quizInit();            

        }

        void checkFile()
        {
            if (File.Exists("quiz.txt"))
                MessageBox.Show("Quiz File Exists");
            else
            {
                StreamWriter quizFile = new StreamWriter("quiz.txt");
                quizFile.Close();
            }
        }

        void quizInit()
        {
            string[] quiz = File.ReadAllLines("quiz.txt");
            string question = "";
            string answear = "";
            int lineCount = quiz.Length;
            if (lineCount == 3)
            {
                question = quiz[lineCount - 2];
                answear = quiz[lineCount - 1];
                qLbl.Text = question;
                aLbl.Text = answear;
            }
            if (lineCount > 3)
            {

                bool approved = false;
                int choises = lineCount / 3;
                int selection = 0;
                int check = 0;
                while(approved == false)
                {
                    selection = rnd.Next(1, 2);
                    check = Convert.ToInt32(quiz[selection * 3 - 3]);
                    if(rnd.Next(1,check) == check) approved = true;
                }
                qLbl.Text = quiz[selection * 3 - 2];
                aLbl.Text = quiz[selection * 3 - 1];
                check++;
                quiz[selection * 3 - 3] = check.ToString();
                File.WriteAllLines("quiz.txt", quiz);
            }
        }

        private void newEntryBtn_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(qTxtbx.Text) &&
                string.IsNullOrWhiteSpace(aTxtbx.Text)) MessageBox.Show("Fill Both Boxes!");
            else
            {
                StreamWriter quizFile = new StreamWriter("quiz.txt", true);
                quizFile.WriteLine((1).ToString());
                quizFile.WriteLine(qTxtbx.Text);
                quizFile.WriteLine(aTxtbx.Text);
                quizFile.Close();
            }
            quizInit();

        }

        private void newBtn_Click(object sender, EventArgs e)
        {
            quizInit();
        }
    }
}

这是输入:

2
q
a
1
q1
a1

Next 中指定的最大值是 Exclusive(https://msdn.microsoft.com/en-us/library/2dx6wyd4(v=vs.110).aspx),这就是为什么 rns,Next(1,2) 总是 1 而 check 总是 2,因为 quiz[0] 是 2。所以最后一个 if 永远不会为真,因为 Next(1,2) 永远不会 2/ 使用 Next(1,3) 它将起作用