如何在我的测验中存储不正确的答案并在第二次尝试中重新填充它们

时间:2017-09-25 11:49:57

标签: c# class methods

我已经在C#中开展了几个星期的测验计划。除了最后一个细节之外,我完全按照它应该工作。我希望能够通过测验一次,然后再次使用任何不正确的答案。在两次尝试结束时,我想向用户显示成绩。有什么想法我怎么能这样做?以下是我正在使用的一些代码:

using System;

namespace Quiz
{
class MultipleChoiceQuiz
{
    public static void CurrentQuestion(string correctAnswer)
    {
        do
        {
            string userAnswer = Console.ReadLine();
            if (userAnswer != "A" && userAnswer != "B" && userAnswer != "C" && userAnswer != "D")
            {
                Console.WriteLine("\nError - Not a Valid Input - Please Enter Valid Input");
            }
            else
            {
                if (userAnswer == correctAnswer)
                {
                    Console.WriteLine("\nThat is correct!");
                    break;
                }
                else if (userAnswer != correctAnswer)
                {
                    Console.WriteLine("\nSorry, that is incorrect.");
                    break;
                }
            }
        }
        while (true);
    }
    public static void Questions()
    {
        Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n");
        Console.WriteLine("Please enter your first name: ");
        string firstName = Console.ReadLine();
        Console.WriteLine("\nWelcome to the HC-130J Power-On Quiz " + firstName + ".\n");
        Console.WriteLine("Using the keyboard, please submit answers by using the \'ENTER\' key.\n");
        Console.WriteLine("Please submit answers in CAPITAL letter form only.\n");
        Console.WriteLine("Ready to begin " + firstName + "? Hit the \'ENTER\' key now...");
        Console.ReadLine();
        Console.Clear();

        //Question 1
        Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n");
        Console.WriteLine("Question 1 - What position does the ramp contol knob need to be in? " +
                          "\n\nA. 3N \nB. 1 \nC. 6N \nD. A or C \n\nWhat is your answer " + firstName + "?");
        CurrentQuestion("D");
        Console.Write("\nPress \'ENTER\' to continue...");
        Console.ReadLine();
        Console.Clear();

        //Question 2
        Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n");
        Console.WriteLine("Question 2 - After power is applied to the aircraft, the battery needs to be turned off? " +
                          "\n\nA. True \nB. False \n\nWhat is your answer " + firstName + "?");
        CurrentQuestion("A");
        Console.Write("\n");
        Console.Write("\nPress \'ENTER\' to continue...");
        Console.ReadLine();
        Console.Clear();

        //Question 3
        Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n");
        Console.WriteLine("Question 3 - Above what temperature does air condition need to be applied to the aircraft while power is applied? " +
                          "\n\nA. 75 degrees Fahrenheit \nB. 100 degrees Fahrenheit \nC. 95 degrees Fahrenheit \nD. 85 degrees Fahrenheit \n\nWhat is your answer " 
                          + firstName + "?");
        CurrentQuestion("C");
        Console.Write("\n");
        Console.Write("\nPress \'ENTER\' to continue...");
        Console.ReadLine();
        Console.Clear();

我有一个班级和两个方法。一种方法是评估测验中的答案是对还是错。另一种是向用户显示所有问题和答案。任何人都可以给我的任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

我们的想法是用适当的结构跟踪答案。人们可以使用字典,例如:

favourite_id

钥匙是你的问题,价值就是你的答案。你可以更多地拥有一个能够保持每个答案正确性的结构。 e.g:

item_id

并且:

Dictionary <string,string> questionsAndAnswers= new Dictionary <string,string>();

答案 1 :(得分:0)

所以你想再次问问玩家回答错误了吗? 为什么不将问题编号添加到列表中,然后在第二次尝试时再次列出该列表。

答案 2 :(得分:0)

面向对象解决此问题的方法包括一个代表您的问题的类以及一个用于保存用户答案的​​数据结构。要将答案与问题相关联,您需要为每个问题添加标识符。下面的代码将允许您使用许多问题扩展此应用程序,而无需复制/粘贴Console.WriteLine块。虽然这似乎更快(并且公平地说我不知道​​你的动机),但考虑可扩展性和维护总是一个好主意。良好的面向对象结构将允许您执行更多操作并编写更少的代码。

public class Question
{
    public int Id { get; set; }

    public string Text { get; set; }

    public IDictionary<string, string> Answers { get; set; }

    public string CorrectAnswer { get; set; }
}

public static class Program
{
    public static IEnumerable<Question> _questions = new []
    {
        new Question
        {
            Id = 1,
            Text = "What position does the ramp contol knob need to be in?",
            Answers = new Dictionary<string, string>
            {,
                { "A", "3N" },
                { "B", "1" },
                { "C", "6N" },
                { "D", "A or C" }
            },
            CorrectAnswer = "D"
        }
    };

    public static IDictionary<int, string> _answers = new Dictionary<int, string>();

    public static void Main()
    {
        Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n");
        Console.WriteLine("Please enter your first name: ");
        string firstName = Console.ReadLine();
        Console.WriteLine("\nWelcome to the HC-130J Power-On Quiz " + firstName + ".\n");
        Console.WriteLine("Using the keyboard, please submit answers by using the \'ENTER\' key.\n");
        Console.WriteLine("Please submit answers in CAPITAL letter form only.\n");
        Console.WriteLine("Ready to begin " + firstName + "? Hit the \'ENTER\' key now...");
        Console.ReadLine();
        Console.Clear();

        foreach(var question in _questions)
        {
            Console.WriteLine(question.Text);
            Console.WriteLine();
            foreach(var answer in question.Answers)
            {
                Console.WriteLine(string.Format("{0}. {1}", answer.Key, answer.Value));
            }
            Console.WriteLine("What is your answer " + firstName + "?");
            var response = Console.ReadLine();
            while(!question.Answers.ContainsKey(response))
            {
                Console.WriteLine("\nError - Not a Valid Input - Please Enter Valid Input");
                response = Console.ReadLine();
            }
            if(response == question.CorrectAnswer)
            {
                Console.WriteLine("\nThat is correct!");
            }
            else
            {
                Console.WriteLine("\nSorry, that is incorrect.");
            }
            _answers[question.Id] = response;
        }

        // do whatever you want when the test is over
    }
}

更多解释:我们创建一个Question类,它有一个标识符,一些文本(要求的实际问题),一组由一个键组成的答案(A,B,C, D等)和一个值(答案的实际文本),以及正确的答案。我们打印出一些初始的,独特的设置说明,其形式是询问用户的姓名,并向他们提供有关如何参加测试的一些提示。

这里的真正价值来自于循环定义良好的对象的能力。我们打印出问题测试,然后打印答案(格式化,以便用户可以理解并适当选择),然后询问用户答案,确定答案的正确性并存储。

希望这有帮助!