2016-08-31 2 views
-2

私が作っているコンソールアプリケーションで助けが必要です。私は勉強に使うためのアプリケーションを作っています。私はそれを始めることができ、私が持っていたい質問をいくつか入力し、質問ごとに質問と回答を入力し、永遠にループして何度も同じ質問を繰り返すループ方法を使いたいと思っています。これは本当に便利だと思った。しかし、途中で問題を抱えています。私はクラスを使用して質問とそれぞれの回答のインスタンスを作成しますが、各質問ごとにインスタンス名を異ならせる方法はわかりません。あなたが別途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"); 
    } 
} 
+1

間違った間違った方法です。すべての質問に対して一意のクラスを作成するわけではありません。無限の質問があり、qusetionオブジェクトごとに「名前を付ける」ために無数の識別子が必要です。あなたは一般的な「質問」オブジェクトを作成します。これは**常に「質問」と呼ばれます。内部的にはそのqusetionにユニークなID(数字は何でも...それはあなた次第です)を与えることができます。 –

+1

あなたはおそらく 'numberofquestions'という値を一つの' question'と 'answer'を含むクラスの中に入れたくないでしょう –

答えて

3

:ここ

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; 


      } 
     } 


    } 
} 
0

以下のコードは、これを試してみてくださいです。

ループ内でこれを行うとき。ループは、前の情報ことを失う繰り返し、一度ので

QuestionAndAnswer question = new QuestionAndAnswer(null,null);

変数は、ループの1回の反復にスコープされます。

ループの前にコレクションを作成します。ループ内で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.questionする必要があります推測している、二回question.answerを設定しています。

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 
+0

' for {{{'と' '}}'が2回ループしています。 ..それはタイプミスですか? :) –

+0

ええええええええええええええええええええええええええええええええええええええええええええええええええええええええ投稿者 –

+0

心配する必要はありません - 私はひどくボックスに直接入力しようとすると苦しんでいます - 勝つためにメモ帳+ +! ;) –

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; 

    } 
} 
関連する問題