私が作っているコンソールアプリケーションで助けが必要です。私は勉強に使うためのアプリケーションを作っています。私はそれを始めることができ、私が持っていたい質問をいくつか入力し、質問ごとに質問と回答を入力し、永遠にループして何度も同じ質問を繰り返すループ方法を使いたいと思っています。これは本当に便利だと思った。しかし、途中で問題を抱えています。私はクラスを使用して質問とそれぞれの回答のインスタンスを作成しますが、各質問ごとにインスタンス名を異ならせる方法はわかりません。あなたが別途QuestionAndAnswer
のコレクションを格納する必要がループの中でクラスのインスタンスを毎回ユニークにする方法
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");
}
}
間違った間違った方法です。すべての質問に対して一意のクラスを作成するわけではありません。無限の質問があり、qusetionオブジェクトごとに「名前を付ける」ために無数の識別子が必要です。あなたは一般的な「質問」オブジェクトを作成します。これは**常に「質問」と呼ばれます。内部的にはそのqusetionにユニークなID(数字は何でも...それはあなた次第です)を与えることができます。 –
あなたはおそらく 'numberofquestions'という値を一つの' question'と 'answer'を含むクラスの中に入れたくないでしょう –