【解決方法】C# でスコアの問題を解決する方法


こんにちは、送信テストメソッドがあり、選択した回答が正しいかどうかに基づいてスコアを割り当てようとしました。 i++ が更新されていてもすべて機能しますが、何らかの理由でスコアの更新が拒否されますか?

どうすればこれを修正できますか

C#
[HttpPost]
        public IActionResult SubmitTest(TestViewModel model)
        {
            // Check if the user has already passed the test
            bool hasPassed = HttpContext.Session.GetBool("TestPassed");

            // If the user has already passed, redirect to the course page
            if (hasPassed)
            {
                return RedirectToAction("TakeCourse", "Courses");
            }

            // Retrieve the module and questions from the database
            Module module = _context.Modules.FirstOrDefault(m => m.ID == model.ModuleID);
            List<Question> questions = GetQuestionsForModule(module);

            // Keep track of the user's score
            int score = 0;

            // Iterate over the questions and check the selected answers
            for (int i = 0; i < questions.Count; i++)
            {
                Question question = questions[i];

                // Get the selected answer(s) for the current question
                int[] selectedAnswerIds = model.Answers
                    .Where(a => a.QuestionID == question.Id)
                    .Select(a => a.ID)
                    .ToArray();

                // Get the answer count for the current question
                int answerCount = GetAnswerCountForQuestion(question.Id);

                // Check if the question is a radio type
                if (question.QuestionType == QuestionType.RADIO)
                {
                    // Retrieve the correct answer for the question
                    Answer correctAnswer = question.Answers.FirstOrDefault(a => a.IsCorrectAnswer);

                    // Check if the selected answer matches the correct answer
                    if (correctAnswer != null && selectedAnswerIds.Length == 1 && correctAnswer.IsCorrectAnswer)
                    {
                        // Increase the score if the selected answer is correct
                        score++;
                    }
                }
                // Check if the question is a checkbox type
                else if (question.QuestionType == QuestionType.CHECKBOX)
                {
                    // Retrieve the correct answers for the question
                    List<Answer> correctAnswers = question.Answers.Where(a => a.IsCorrectAnswer).ToList();

                    // Check if all selected answers are correct and no incorrect answers are selected
                    if (correctAnswers.Count == selectedAnswerIds.Length &&
                        selectedAnswerIds.All(id => correctAnswers.Any(a => a.ID == id && a.IsCorrectAnswer)))
                    {
                        // Increase the score if all selected answers are correct
                        score++;
                    }
                }
            }

            
            
            // Determine if the user passed or failed the test based on the score
            bool passed = score == questions.Count;

            // If the user passed, store the information in the session and redirect to the course page
            if (passed)
            {
                HttpContext.Session.SetBool("TestPassed", true);
                return RedirectToAction("TakeCourse", "Courses");
            }
            else
            {
                // Redirect back to the test page with a failure message
                TempData["TestResult"] = "You failed the test.";
                return RedirectToAction("TakeTest", new { moduleId = model.ModuleID });
            }
        }

私が試したこと:

小さなステップごとにブレークポイントを使用してデバッグを試みました。

すべての正しい値が実際に取得されていることを何度も再検査しました。

i++ 部分は正しく更新されていますが、スコア値は更新されていません。

解決策 1

私たちにはわかりません。私たちはあなたのデータにアクセスできません。これは、コードが実行していることを正確に再現するために必要です。

それはあなた次第です。
幸いなことに、何が起こっているかを調べるのに役立つツール、デバッガーが利用可能です。 使い方がわからない場合は、Google で「Visual Studio デバッガー」を検索すると必要な情報が得られます。

関数の最初の行にブレークポイントを設定し、デバッガーでコードを実行します。 次に、コードとデータを調べて、手動で何が起こるかを考えます。 次に、各行を 1 ステップずつ実行して、期待したことが実際に起こったかどうかを確認します。 そうでない場合は、問題が発生しているときなので、前に戻って (またはもう一度実行して詳しく調べて) 理由を調べることができます。

ブレークポイントを使用して、score++ が最初に実行されているかどうかを確認します。実行されていない可能性もあります。 そのため、ブレークポイントがヒットするポイントに到達するまでブレークポイントの移動を開始し、スコアの更新を続行する必要があると考えます。 次に、単一ステップでどのパスをたどっているかを正確に特定し、関連する変数を調べて、期待どおりにいかない理由を調べます。

申し訳ありませんが、私たちはそれを行うことはできません。新しい (そして非常に便利な) スキルであるデバッグを学ぶ時期です。

コメント

タイトルとURLをコピーしました