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

C# 如何使类的实例在循环中每次都是唯一的,c#,class,C#,Class,我需要一个控制台应用程序的帮助。我正在申请学习用的。我希望能够启动它,输入我想要的问题数量,为每个问题输入问题和答案,使用一种循环的方法,将永远循环并一遍又一遍地问相同的问题。我觉得这真的很有用。但是我在路上遇到了一个问题。我使用一个类为问题及其各自的答案创建实例,但我不知道如何使每个问题的实例名称不同。下面是代码 namespace glosor { class Program { static void Main(string[] args)

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

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;


            }
        }


    }
}
试试这个:

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");
    }
} 
List qList=new List();
静态void Main(字符串[]参数)
{
Console.WriteLine(“您好,请输入您希望本次重新考试有多少个问题”);
int numberofquestions=Convert.ToInt32(Console.ReadLine());

对于(int i=0;i您需要分别存储一组
问题和回答

当您在循环中执行此操作时

QuestionAndAnswer question=newquestionandanswer(null,null);

变量的作用域是循环的一次迭代,因此一旦循环重复,您就失去了先前的信息

在循环之前创建一个集合,在循环中将您的
问题和回答对象添加到
列表中
。循环完成后,
列表中的信息将可用

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

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

您可能应该重新考虑一下您试图完成的任务。在不过度更改代码的情况下,您可以输入问题和答案,并将其添加到列表中,直到您键入“完成”一词。然后会反复提示这些问题,直到您键入“退出”一词。希望这能为您提供一些扩展思路

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;

    }
}
类程序
{
静态void Main(字符串[]参数)
{
//使用列表收集您的问题和回答对象
列表问题=新列表();
Console.WriteLine(“您好,请输入您希望本次重新考试有多少个问题”);
int-questionCount=1;
//循环直到您键入完毕
while(true)
{
Console.WriteLine(“您希望问题编号“+questionCount+”是什么?”);
QuestionAndAnswer question=新的QuestionAndAnswer(空,空);
字符串响应=Console.ReadLine();
如果(response.ToUpper()=“FINISHED”)中断;
问题=回答;
Console.WriteLine(“您希望问题编号“+questionCount+”的答案是什么?”);
question.answer=Console.ReadLine();
//将问题(问题和答案)添加到列表中
问题.加入(问题);
question.questionNumber=问题计数;
问题计数++;
}
//本节将询问这些问题,直到您键入exit
while(true)
{
foreach(问题和回答问题中的qa)
{
WriteLine(String.Format({1}中的问题{0}:{2}),qa.questionNumber,questions.Count(),qa.Question));
控制台。写(“键入您的答案并点击:”);
如果(Console.ReadLine().ToUpper()=“退出”)中断;
控制台。写线(qa。应答);
}
}
}
}
课堂提问与回答
{
公共字符串问题;
公共字符串应答;
公众电话号码;
公开问题和回答(字符串-问题,字符串-答案)
{
问题=_问题;
答案=_答案;
}
}

错误的处理方式。你没有为每个问题创建一个唯一的类。问题的数量是无限的,你需要无限的标识符来“命名”每个qusetion对象。你创建了一个通用的“问题”对象,通常称为“问题”。在内部,您可以给该问题一个唯一的ID(一个数字?随便什么…由您决定)因此,您可以唯一地将一个问题与另一个问题区分开来。您可能不希望值
numberofquestions
位于包含单个
问题
答案
的类中。您在
for
循环中有2个
{
和2个
}
循环,这是一个打字错误吗?:)是的-谢谢…试图直接在回答框中键入内容把我搞砸了。不用担心-我很痛苦试图直接在回答框中键入内容-记事本++获胜!;)顺便说一句-最好在这里加入一个快速持久化方法,这样你就不必每次都键入列表。只需使用TextWriter或其他方法保存到磁盘。除非f QA很小,你想每次都把它们打进去。@GeoffJames-你的意思是OP而不是“你”。啊。我今天忙得不可开交——下山撞上了砖墙。
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;

    }
}