2017-02-07 6 views
0

いくつかの属性を持つ質問オブジェクトを作成しようとしています。これらの属性はすべてテキストファイルに保存され、StreamReaderを使用して取得されます。ストリームライターでforループを使用する

属性の1つはChoices[]です。変数qTypeの値に基づいて値を設定できるようにするには、配列が必要です。

たとえば、if qType = "button"の場合、Choices[]はファイルから4行だけを読み取る必要があります。一方、if qType = "dragdrop"の場合、Choices[]はファイルから6回読み取る必要があります。私はこれまで、forループ、case文、if文を使用しようとしましたが、すべてが中断しています。誰かが私がStreamReaderを混乱させることなくこれをどうやってできるか教えてもらえますか?

HERESに動作するコード:

Choices = new string[] 
{ 
    for(int i=0; i<4; i++) 
    { 
     quizFileReader.ReadLine(), // expects a ; 
    } 
}, // excepts a ; 

私が使用していますHERESに完全なコード:

public string QuestionText, imgPath, hintTxt; // Actual question text. 
    public string[] Choices; // Array of answers from which user can choose. 
    public int Answer, qNum, linesToRead;   // Index of correct answer within Choices. 
    public double difficulty; // Double that represents difficulty of each question 

    public List<Question> getQues() // reads questions from text file, assigns all strings in text file to index of List, returns the full list of questions 
    { 
     // Create new list to store all questions. 
     var questions = new List<Question>(); 

     // Open file containing quiz questions using StreamReader, which allows you to read text from files easily. 
     using (var quizFileReader = new System.IO.StreamReader("PhysQuestions.txt")) 
     { 
      string line; 
      Question question; 

      // Loop through the lines of the file until there are no more (the ReadLine function return null at this point). 
      // ReadLine called here only reads question texts (first line of a question), while other calls to ReadLine read the choices. 
      while ((line = quizFileReader.ReadLine()) != null) 
      { 
       // Skip this loop if the line is empty. 
       if (line.Length == 0) 
        continue; 

       // Create a new question object. 
       // The "object initializer" construct is used here by including { } after the constructor to set variables. 
       question = new Question() 
       { 
        // Set the question text to the line just read. 
        QuestionText = line, 
        linesToRead = Convert.ToInt32(quizFileReader.ReadLine()), 
        Choices = new string[linesToRead]; 
        for (int i=0; i < linesToRead; i++) 
        { 
         Choices[i] = await quizFileReader.ReadLineAsync(); 
        } 


        hintTxt = await quizFileReader.ReadLineAsync(); 
        difficulty = Convert.ToDouble(quizFileReader.ReadLine()); 
        imgPath = quizFileReader.ReadLine(); 

       }; 

       // Set correct answer to -1, this indicates that no correct answer has been found yet. 
       question.Answer = -1; 

       // Check each choice to see if it begins with the '!' char (marked as correct). 
       for (int i = 0; i < 4; i++) 
       { 
        if (question.Choices[i].StartsWith("!")) 
        { 
         // Current choice is marked as correct. Therefore remove the '!' from the start of the text and store the index of this choice as the correct answer. 
         question.Choices[i] = question.Choices[i].Substring(1); 
         question.Answer = i; 
         break; // Stop looking through the choices. 
        } 
       } 

       // Check if none of the choices was marked as correct. If this is the case, we throw an exception and then stop processing. 
       if (question.Answer == -1) 
       { 
        throw new InvalidOperationException(
         "No correct answer was specified for the following question.\r\n\r\n" + question.QuestionText); 
       } 






       // Finally, add the question to the complete list of questions. 
       questions.Add(question); 
      } 

      return questions; 
     } 
    } 

using (var quizFileReader = new System.IO.StreamReader("PhysQuestions.txt")) 
{ 
    string line; 
    Question question; 

    // Loop through the lines of the file until there are no more (the ReadLine function return null at this point). 
    // ReadLine called here only reads question texts (first line of a question), while other calls to ReadLine read the choices. 
    while ((line = quizFileReader.ReadLine()) != null) 
    { 
     // Skip this loop if the line is empty. 
     if (line.Length == 0) 
      continue; 

     // Create a new question object. 
     // The "object initializer" construct is used here by including { } after the constructor to set variables. 
     question = new Question() 
     { 
      // Set the question text to the line just read. 
      QuestionText = line, 
      qType = quizFileReader.ReadLine(), 
      // Set the choices to an array containing the next 4 lines read from the file. 
      Choices = new string[] 
      { 
       quizFileReader.ReadLine(), 
       quizFileReader.ReadLine(), 
       quizFileReader.ReadLine(), 
       quizFileReader.ReadLine(), 
      }, 

      hintTxt = quizFileReader.ReadLine(), 
      difficulty = Convert.ToDouble(quizFileReader.ReadLine()), 
      imgPath = quizFileReader.ReadLine() 
     }; 
    } 
} 

は、ここで私はそれが動作しません行うことを試みている何か

答えて

0

読み取る線の数がわかったら、

using (var quizFileReader = new System.IO.StreamReader("PhysQuestions.txt")) 
{ 
    Question question; 
    int linesToRead = 4; 
    string[] choices = new string[linesToRead]; 

    for (int i=0; i < linesToRead; i++) 
    { 
     choices[i] = await quizFileReader.ReadLineAsync(); 
    } 
} 

これはあなたが正しい方向にスタートするはずですが、どこかにつかれたら教えてください。

ReadLineを非同期バージョンに変換してブロックしないようにしました。

+0

こんにちは、これを手伝ってくれてありがとう、私は本当にそれが必要です。申し訳ありませんが、私はC#の初心者です。「await」と「async」に慣れていません。私があなたのコードを私の中に実装しようとすると、このコードが非同期にあるメソッドを作るように頼んだエラーが発生しました。メソッドが値(つまりList qList)を返すことができる必要があるため、これを行うことはできません。私はあなたに私のコードの残りの部分を与えて、私が意味することを見ることができます。再度、感謝します。 – anonymous2506

+0

ああ、もしあなたが非同期ではないならば、 'choices [i] = await quizFileReader.ReadLineAsync();'を 'choices [i] = quizFileReader.ReadLine();に変更してブロックさせてください。 Asyncはもう一つの獣です。あなたが最初に勉強しているときに心配する必要はありません。 :) –

+0

申し訳ありません、選択肢を定義してからforループが私のためにストリームを壊します – anonymous2506

関連する問題