永久在while循环c#,Visual Studio 2013中

时间:2023-01-19 20:53:48

with my code, I'm supposed to be able to call a question from my file at random and not reuse that question again.

使用我的代码,我应该能够随机地从我的文件中调用一个问题,而不是再次重用该问题。

But for some reason I'm stuck in the while loop and after a few hours of searching I still can't see why, so I hope you guys can help me out. (he gets stuck after he generated 8 questions)

但由于某种原因,我陷入了while循环,经过几个小时的搜索,我仍然看不出原因,所以我希望你们能帮助我。 (他在生成8个问题后陷入困境)

Code (for the generatequestion one, if you need more I can paste it but I hope this gives enough information):

代码(对于generatequestion一,如果你需要更多,我可以粘贴它,但我希望这提供了足够的信息):

lineCount = File.ReadLines(pad).Count();
questions = new string[lineCount, 2];


public string GenerateQuestion(int total)
    {
        if (total <= 10)
        {
            Random ran = new Random();
            questionNumber = ran.Next(lineCount);
            while (previousQuestions.Contains(questionNumber))
            {
                questionNumber = ran.Next(lineCount);
            }

        }
        previousQuestions[questionCount] = questionNumber;
        questionCount++;
        return questions[questionNumber, 0];
    }

2 个解决方案

#1


You could make a method to return the lines in the file in random order.

您可以创建一个方法以随机顺序返回文件中的行。

public string[] GetQuestionsInRandomOrder()
{
    var lines = File.ReadAllLines("test.txt");
    var rnd = new Random();
    lines = lines.OrderBy(line => rnd.Next()).ToArray();
    return lines;
}

Then as you use the questions you can remove them from the array.

然后,当您使用问题时,可以从阵列中删除它们。

var isRemoved = Array.remove(array, item);

https://msdn.microsoft.com/en-us/library/vstudio/bb397721%28v=vs.100%29.aspx

This is easier that what I had before and will give greater control. I would create a class for this and this example is a starting point. You can add more functionality as you go along. By having a class to do all this work you can add methods to do additional features later on without having to rewrite code.

这比以前更容易,并且会给予更好的控制。我会为此创建一个类,这个例子是一个起点。您可以随时添加更多功能。通过让一个类完成所有这些工作,您可以在以后添加方法来执行其他功能,而无需重写代码。

public class Logic
{
    public List<string> AllQuestionsInRandomOrder { get; set; } 
    public List<string> QuestionsThatHaveBeenRemoved { get; set; }

    public Logic()
    {
        QuestionsThatHaveBeenRemoved = new List<string>();
        AllQuestionsInRandomOrder = GetQuestionsInRandomOrder().ToList();
    }

    public string GetUnusedQuestion()
    {
        var question =
            AllQuestionsInRandomOrder.FirstOrDefault(x => !QuestionsThatHaveBeenRemoved.Contains(x));
        QuestionsThatHaveBeenRemoved.Add(question);
        return question;
    }

    public IEnumerable<string> GetQuestionsInRandomOrder()
    {
        var lines = File.ReadAllLines("test.txt").ToList();
        var rnd = new Random();
        lines = lines.OrderBy(line => rnd.Next()).ToList();
        return lines;
    }

    public void RemoveQuestion(List<string> questions, string questionToRemove)
    {
        questions.Remove(questionToRemove);
    }
}         

I would avoid using loops and break the problem into separate methods or functions. It will make it easier to debug. This implementation does not separate the questions and answers out, so you will need to add a method that returns a dictionary of the questions and answers.

我会避免使用循环并将问题分解为单独的方法或函数。它将使调试更容易。此实现不会将问题和答案分开,因此您需要添加一个返回问题和答案字典的方法。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            var questions = new Questions();
            var firstQuestion = questions.GetUnusedQuestion();
            Console.WriteLine(firstQuestion);
        }
    }

    class Questions
    {

        public string FileName { get; set; }
        public static List<string> AllQuestionsAndAnswersFromFile { get; set; } 
        public List<string> AllQuestionsInRandomOrder { get; set; }
        public List<string> QuestionsThatHaveBeenRemoved { get; set; }

        public Questions()
        {
            FileName = "text.txt";
            QuestionsThatHaveBeenRemoved = new List<string>();
            AllQuestionsAndAnswersFromFile = new List<string>();
            ReadQuestionsFromFile();
            AllQuestionsInRandomOrder = GetQuestionsInRandomOrder().ToList();
        }

        public string GetUnusedQuestion()
        {
            var question =
                AllQuestionsInRandomOrder.FirstOrDefault(x => QuestionsThatHaveBeenRemoved.Contains(x));
            QuestionsThatHaveBeenRemoved.Add(question);
            return question;
        }

        private static IEnumerable<string> GetQuestionsInRandomOrder()
        {
            var lines = AllQuestionsAndAnswersFromFile;
            var rnd = new Random();
            lines = lines.OrderBy(line => rnd.Next()).ToList();
            return lines;
        }

        public void RemoveQuestion(List<string> questions, string questionToRemove)
        {
            questions.Remove(questionToRemove);
        }

        public void ReadQuestionsFromFile()
        {
            using (var reader = new StreamReader(FileName, Encoding.Default))
            {
                var text = reader.ReadToEnd();
                var lines = text.Split('=');
                foreach (var s in lines)
                {
                    AllQuestionsAndAnswersFromFile.Add(s);
                }
            }
        }
    }
}

#2


When you run out of questions, let's see what

当你用完问题时,让我们看看是什么

while (previousQuestions.Contains(questionNumber))
{
    questionNumber = ran.Next(lineCount);
}

actually does:

  • Is questionNumber one of the questions we already asked?
  • questionNumber是我们已经提出的问题之一吗?

  • Yes, get a new question number. (because we've asked all the questions)
  • 是的,获得一个新的问题编号。 (因为我们已经问过所有问题)

  • Is questionNumber one of the questions we already asked?
  • questionNumber是我们已经提出的问题之一吗?

  • yes, get a new question number. (because we've asked all the questions)
  • 是的,得到一个新的问号。 (因为我们已经问过所有问题)

A solution in this case would be to shuffle your questions that you return, removing them as you grab them.

在这种情况下,解决方案是将您返回的问题随机化,并在抓取它们时删除它们。

#1


You could make a method to return the lines in the file in random order.

您可以创建一个方法以随机顺序返回文件中的行。

public string[] GetQuestionsInRandomOrder()
{
    var lines = File.ReadAllLines("test.txt");
    var rnd = new Random();
    lines = lines.OrderBy(line => rnd.Next()).ToArray();
    return lines;
}

Then as you use the questions you can remove them from the array.

然后,当您使用问题时,可以从阵列中删除它们。

var isRemoved = Array.remove(array, item);

https://msdn.microsoft.com/en-us/library/vstudio/bb397721%28v=vs.100%29.aspx

This is easier that what I had before and will give greater control. I would create a class for this and this example is a starting point. You can add more functionality as you go along. By having a class to do all this work you can add methods to do additional features later on without having to rewrite code.

这比以前更容易,并且会给予更好的控制。我会为此创建一个类,这个例子是一个起点。您可以随时添加更多功能。通过让一个类完成所有这些工作,您可以在以后添加方法来执行其他功能,而无需重写代码。

public class Logic
{
    public List<string> AllQuestionsInRandomOrder { get; set; } 
    public List<string> QuestionsThatHaveBeenRemoved { get; set; }

    public Logic()
    {
        QuestionsThatHaveBeenRemoved = new List<string>();
        AllQuestionsInRandomOrder = GetQuestionsInRandomOrder().ToList();
    }

    public string GetUnusedQuestion()
    {
        var question =
            AllQuestionsInRandomOrder.FirstOrDefault(x => !QuestionsThatHaveBeenRemoved.Contains(x));
        QuestionsThatHaveBeenRemoved.Add(question);
        return question;
    }

    public IEnumerable<string> GetQuestionsInRandomOrder()
    {
        var lines = File.ReadAllLines("test.txt").ToList();
        var rnd = new Random();
        lines = lines.OrderBy(line => rnd.Next()).ToList();
        return lines;
    }

    public void RemoveQuestion(List<string> questions, string questionToRemove)
    {
        questions.Remove(questionToRemove);
    }
}         

I would avoid using loops and break the problem into separate methods or functions. It will make it easier to debug. This implementation does not separate the questions and answers out, so you will need to add a method that returns a dictionary of the questions and answers.

我会避免使用循环并将问题分解为单独的方法或函数。它将使调试更容易。此实现不会将问题和答案分开,因此您需要添加一个返回问题和答案字典的方法。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            var questions = new Questions();
            var firstQuestion = questions.GetUnusedQuestion();
            Console.WriteLine(firstQuestion);
        }
    }

    class Questions
    {

        public string FileName { get; set; }
        public static List<string> AllQuestionsAndAnswersFromFile { get; set; } 
        public List<string> AllQuestionsInRandomOrder { get; set; }
        public List<string> QuestionsThatHaveBeenRemoved { get; set; }

        public Questions()
        {
            FileName = "text.txt";
            QuestionsThatHaveBeenRemoved = new List<string>();
            AllQuestionsAndAnswersFromFile = new List<string>();
            ReadQuestionsFromFile();
            AllQuestionsInRandomOrder = GetQuestionsInRandomOrder().ToList();
        }

        public string GetUnusedQuestion()
        {
            var question =
                AllQuestionsInRandomOrder.FirstOrDefault(x => QuestionsThatHaveBeenRemoved.Contains(x));
            QuestionsThatHaveBeenRemoved.Add(question);
            return question;
        }

        private static IEnumerable<string> GetQuestionsInRandomOrder()
        {
            var lines = AllQuestionsAndAnswersFromFile;
            var rnd = new Random();
            lines = lines.OrderBy(line => rnd.Next()).ToList();
            return lines;
        }

        public void RemoveQuestion(List<string> questions, string questionToRemove)
        {
            questions.Remove(questionToRemove);
        }

        public void ReadQuestionsFromFile()
        {
            using (var reader = new StreamReader(FileName, Encoding.Default))
            {
                var text = reader.ReadToEnd();
                var lines = text.Split('=');
                foreach (var s in lines)
                {
                    AllQuestionsAndAnswersFromFile.Add(s);
                }
            }
        }
    }
}

#2


When you run out of questions, let's see what

当你用完问题时,让我们看看是什么

while (previousQuestions.Contains(questionNumber))
{
    questionNumber = ran.Next(lineCount);
}

actually does:

  • Is questionNumber one of the questions we already asked?
  • questionNumber是我们已经提出的问题之一吗?

  • Yes, get a new question number. (because we've asked all the questions)
  • 是的,获得一个新的问题编号。 (因为我们已经问过所有问题)

  • Is questionNumber one of the questions we already asked?
  • questionNumber是我们已经提出的问题之一吗?

  • yes, get a new question number. (because we've asked all the questions)
  • 是的,得到一个新的问号。 (因为我们已经问过所有问题)

A solution in this case would be to shuffle your questions that you return, removing them as you grab them.

在这种情况下,解决方案是将您返回的问题随机化,并在抓取它们时删除它们。