如何在循环中使每个类的实例都是唯一的

时间:2016-08-31 16:02:29

标签: c# class

我需要帮助我正在制作的控制台应用程序。我正在申请学习。我希望能够启动它,输入我想要的问题数量,输入每个问题的问题和答案,使用循环方法永远循环并一遍又一遍地问同样的问题。认为这将非常有用。但我在途中遇到了问题。我使用一个类来为问题及其各自的答案创建实例,但我不知道如何使每个问题的实例名称不同。这是下面的代码

namespace glosor
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("Hello, please enter how many questions you would like to have on this reapeting test");
            int numberofquestions = Convert.ToInt32(Console.ReadLine());
           while(numberofquestions > 0)
            {
                Console.WriteLine(" what do you want question number " + numberofquestions + " to be?");

                QuestionAndAnswer question = new QuestionAndAnswer(null,null);
                question.answer = Console.ReadLine();
                Console.WriteLine(" what do you want question number " + numberofquestions + "'s answer to be?");
                question.answer = Console.ReadLine();
                numberofquestions--;
                QuestionAndAnswer.numberofquestions++;
            }


        }


         class QuestionAndAnswer{
            public string question;
            public string answer;
            public static int numberofquestions;
            public QuestionAndAnswer(string _question,string _answer)
            {
                question = _question;
                answer = _answer;


            }
        }


    }
}

3 个答案:

答案 0 :(得分:3)

您需要单独存储QuestionAndAnswer的集合。

在循环中执行此操作。

QuestionAndAnswer question = new QuestionAndAnswer(null,null);

变量的范围是循环的单个迭代,所以一旦循环重复,你就会丢失之前的信息。

在循环之前创建一个集合,在循环中将QuestionAndAnswer个对象添加到List。循环完成后,List中的信息将可用。

List<QuestionAndAnswer> allQuestionAndAnswers = new List<QuestionAndAnswer>();

while(numberofquestions > 0)
{
    ...other code here
    QuestionAndAnswer question = new QuestionAndAnswer("The question", "The answer");
    allQuestionAndAnswers.Add(question);
}

此外,您要设置question.answer两次,我猜第一个应该是question.question

QuestionAndAnswer question = new QuestionAndAnswer(null,null);
question.answer = Console.ReadLine(); //Once here
Console.WriteLine(" what do you want question number " + numberofquestions + "'s answer to be?");
question.answer = Console.ReadLine(); //And again a second time

答案 1 :(得分:0)

试试这个:

List<QuestionAndAnswer> qList = new List<QuestionAndAnswer>();
static void Main(string[] args)
{
    Console.WriteLine("Hello, please enter how many questions you would like to have on this reapeting test");
    int numberofquestions = Convert.ToInt32(Console.ReadLine());
   for(int i=0;i<numberofquestions;i++){        
        Console.WriteLine(" what do you want question number " + i.ToString() + "'s QUESTION to be?");

        QuestionAndAnswer question = new QuestionAndAnswer(null,null);
        question.question = Console.ReadLine();
        Console.WriteLine(" what do you want question number " + i.ToString() + "'s ANSWER to be?");
        question.answer = Console.ReadLine();
        qList.Add(question);
        //NumberOfQuestions is now qList.Count
    }

    startFlash();
}
private static void startFlash(){
    string hell = "hot";
    Random rnd = new Random();
    while(hell!="freezing"){

    int index = rnd.Next(qList.Count);
    QuestionAnswer qA = qList[index];
    Console.WriteLine("Question "+index.ToString()+": "+qA.question);
    Console.ReadKey(true);
    Console.WriteLine(Answer: "+qA.answer+"\n");
    }
} 

答案 2 :(得分:0)

您可能应该重新考虑您的方法,以达到您想要完成的目标。在不更改代码的情况下,输入您添加到列表中的问题和答案,直到您键入完成后的单词。然后反复提示问题直到您键入单词exit。希望这能为你提供一些扩展的想法。

class Program
{

    static void Main(string[] args)
    {
        //Use a list to collect your QuestionAndAnswer objects
        List<QuestionAndAnswer> questions = new List<QuestionAndAnswer>();
        Console.WriteLine("Hello, please enter how many questions you would like to have on this reapeting test");
        int questionCount = 1;

        // loops until you type finished
        while (true)
        {
            Console.WriteLine(" what do you want question number " + questionCount + " to be?");
            QuestionAndAnswer question = new QuestionAndAnswer(null, null);
            string response = Console.ReadLine();
            if (response.ToUpper() == "FINISHED") break;
            question.question = response;
            Console.WriteLine(" what do you want question number " + questionCount + "'s answer to be?");
            question.answer = Console.ReadLine();
            //Add the question (QuestionAndAnswer) to the list
            questions.Add(question);
            question.questionNumber = questionCount;
            questionCount++;
        }

        //This section will ask the questions until you type exit
        while (true)
        {
            foreach (QuestionAndAnswer qa in questions)
            {
                Console.WriteLine(String.Format("Question #{0} out of {1}: {2}", qa.questionNumber, questions.Count(), qa.question));
                Console.Write("Type you answer and hit <Enter>: ");
                if(Console.ReadLine().ToUpper()=="EXIT") break;
                Console.WriteLine(qa.answer);

            }
        }

    }
}


class QuestionAndAnswer
{
    public string question;
    public string answer;
    public int questionNumber;
    public QuestionAndAnswer(string _question, string _answer)
    {
        question = _question;
        answer = _answer;

    }
}