Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#。我如何存储错误答案,以便在用户完成测验后使用?_C# - Fatal编程技术网

C#。我如何存储错误答案,以便在用户完成测验后使用?

C#。我如何存储错误答案,以便在用户完成测验后使用?,c#,C#,我需要此测试来存储用户错误回答的问题,然后在测试结束时,程序将为用户提供查看错误回答的问题并再次执行的选项。有人能给我解释一下怎么做或者给我指出正确的方向吗 先谢谢你 这就是我目前所拥有的 enter code here using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Quiz {

我需要此测试来存储用户错误回答的问题,然后在测试结束时,程序将为用户提供查看错误回答的问题并再次执行的选项。有人能给我解释一下怎么做或者给我指出正确的方向吗

先谢谢你

这就是我目前所拥有的

enter code here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quiz
{
    public class User
    {
        public static string name;
        public User()
        {
        string quiz = "This Quiz consists of 10 questions. \r\n5 True or False and 5 Multiple choice.";
        string user1 = "Please enter your name (last, first) and press ENTER.";
        string heading1 = String.Format("{0}\r\n{1}\r\n{2}", heading, quiz, user1);
        Console.WriteLine(heading1);
        name = Console.ReadLine();
        Console.Clear();

        string introduction = "Welcom to the Aeronautical Knowledge Quiz " + name + "!" + "\r\nNOTE: This quiz is case sensitive.";
        string enter = "Press ENTER to begin the quiz.";
        string introduction1 = String.Format("{0}\r\n{1}", introduction, enter);
        Console.WriteLine(introduction1);
        Console.ReadLine();
        Console.Clear();

    }

}

public class Quiz
{

    public static void Main()
    {
        User user1 = new User();


        string[] questions = { "The fuselage is the center structure of an aircraft and provides the connection for the wings and tail. \r\nTrue or False?",
            "Rolling is the action on the lateral axis of an aircraft. \r\nTrue or False?",

            "Drag is the name of the force that resists movement of an aircraft through the air. \r\nTrue or False?",

            "Flaps are attached to the trailing edge of a wing structure and only increases drag. \r\nTrue or False?",

            "Powerplant or engine produces thrust to propel an aircraft. \r\nTrue or False?",

            "Which of the following are part of an aircraft primary flight controls? \r\na.Aileron. \r\nb.Rudder. \r\nc.Elevators. \r\nd. All of the above.",
            "The Fuel-air control unit of a reciprocating engine? \r\na.Sends fuel to the piston chamber. \r\nb.Sends air to the piston chamber. \r\nc.Controls the mixture of air and fuel. \r\nd.Meters the quantity of fuel.",

            "Which of the following is the main source of electrical power when starting an aircraft? \r\na.Primary Bus. \r\nb.Avionics Bus. \r\nc.Battery. \r\nd.GPU (ground power unit)",

            "The reservoir of a hydraulic system is used for? \r\na.Store and collect fluid from a hydraulic system. \r\nb.Lubricate components when needed. \r\nc.Keep the fluid clean. \r\nd.All of the above.",

            "Flying into fog can create? \r\na.Narrows the runway. \r\nb.An aircraft to stall. \r\nc.An illusion of pitching up. \r\nd.A stressful environment for the Pilot and Co-pilot." };

        string[] answers = { "True", "True", "True", "False", "True", "d", "c", "c", "a", "c" };
        string studentAnswer;
        int correctAnswer = 0;
        int qcounter = 0;
        int questionNum = 0;
        int answerNum = 0;
        while (questionNum < questions.Length)
        {
            Console.WriteLine(questions[questionNum], 10, 30);
            studentAnswer = Console.ReadLine();
            if (studentAnswer == answers[answerNum])
            {
                Console.WriteLine("Correct!");
                questionNum++;
                answerNum++;
                correctAnswer++;
                qcounter++;
            }
            else
            {
                Console.WriteLine("Incorrect.");
                questionNum++;
                answerNum++;
                qcounter++;
            }
            Console.WriteLine();
            Console.WriteLine("Press ENTER for Next question.");
            Console.ReadLine();
            Console.Clear();

        }
        Console.WriteLine(User.name + ", Your final score is: " + correctAnswer + "/" + qcounter + ".");
        Console.WriteLine();
        Console.WriteLine("Press ENTER to EXIT");

        Console.ReadKey(true);
    }
}
在此处输入代码
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间测验
{
公共类用户
{
公共静态字符串名;
公共用户()
{
字符串测验=“此测验由10个问题组成。\r\n5个正确或错误以及5个多项选择。”;
string user1=“请输入您的姓名(姓氏、姓氏),然后按enter键。”;
string heading1=string.Format(“{0}\r\n{1}\r\n{2}”,标题,测验,user1);
控制台写入线(标题1);
name=Console.ReadLine();
Console.Clear();
string introduction=“欢迎参加航空知识测验”+name+“!”+“\r\n注意:此测验区分大小写。”;
string enter=“按enter开始测验。”;
string introduction1=string.Format(“{0}\r\n{1}”,introduction,回车);
控制台写入线(简介1);
Console.ReadLine();
Console.Clear();
}
}
公开课测验
{
公共静态void Main()
{
用户user1=新用户();
string[]questions={“机身是飞机的中心结构,为机翼和机尾提供连接。\r\n是正确的还是错误的?”,
“滚转是飞机横轴上的动作。\r\n是“滚转”还是“滚转”,
“阻力是阻止飞机在空中移动的力的名称。\r\n阻力是“阻力”还是“阻力”,
“襟翼连接到机翼结构的后缘,只会增加阻力。\r\n是错误还是错误?”,
“动力装置或发动机产生推进飞机的推力。\r\n推力是假的?”,
“以下哪项是飞机主要飞行控制系统的一部分?\r\n副翼。\r\nb.方向舵。\r\nc.升降舵。\r\n上述所有部件。”,
“往复式发动机的燃油空气控制单元?\r\n a.向活塞室输送燃油。\r\nb.向活塞室输送空气。\r\nc.控制空气和燃油的混合。\r\n d.计量燃油量。”,
“启动飞机时,下列哪项是主要电源?\r\n主总线。\r\nb.航空电子总线。\r\nc.电池。\r\nd.GPU(地面电源装置)”,
“液压系统的储液罐用于?\r\n存储和收集液压系统中的油液。\r\nb.必要时润滑部件。\r\nc.保持油液清洁。\r\n以及上述所有内容。”,
“飞入雾中会造成?\r\n A.跑道变窄。\r\n b.飞机失速。\r\n c.俯仰的错觉。\r\n d.飞行员和副飞行员的紧张环境。”};
字符串[]答案={“真”、“真”、“真”、“假”、“真”、“d”、“c”、“c”、“a”、“c”};
弦乐学生问答;
int correctAnswer=0;
int qcounter=0;
int questionNum=0;
int answerNum=0;
while(questionNum

}步骤

  • 声明studentAnswer空数组(使用相同长度的答案数组)
  • 使用相同的答案数组索引并存储控制台。readline输入
这是要编辑的代码的一部分(它将存储所有答案)

狗仔队评论后编辑:

此处存储所有不正确的答案:

//Declare the array with answer
string[] answers = { "True", "True", "True", "False", "True", "d", "c", "c", "a", "c" };

string[] studentAnswer = new string[answers.Length];
//You can use list
List<string> incorrectAnswer = new List<string>();
//Use the array in you while loop
while (questionNum < questions.Length)
        {
            Console.WriteLine(questions[questionNum], 10, 30);
            //Here you store the student answer
            studentAnswer = Console.ReadLine();
            //Here you check the student answer using the same index of answer array
            if (studentAnswer.ToLower() == answers[answerNum].ToLower())
            {
                Console.WriteLine("Correct!");
                questionNum++;
                answerNum++;
                correctAnswer++;
                qcounter++;
            }
            else
            {
                Console.WriteLine("Incorrect.");
                incorrectAnswer.Add(studentAnswer)
                //Remove these increment and the question will be the same in the next loop cycle
                questionNum++; // remove
                answerNum++; // remove
                qcounter++; // remove
            }
            Console.WriteLine();
            Console.WriteLine("Press ENTER for Next question.");
            Console.ReadLine();
            Console.Clear();
        }
//用应答声明数组
字符串[]答案={“真”、“真”、“真”、“假”、“真”、“d”、“c”、“c”、“a”、“c”};
string[]studentAnswer=新字符串[answers.Length];
//您可以使用列表
List incorrectAnswer=新列表();
//在while循环中使用数组
while(questionNumif (studentAnswer[answerNum].ToLower() == answers[answerNum].ToLower())
//Declare the array with answer
string[] answers = { "True", "True", "True", "False", "True", "d", "c", "c", "a", "c" };

string[] studentAnswer = new string[answers.Length];
//You can use list
List<string> incorrectAnswer = new List<string>();
//Use the array in you while loop
while (questionNum < questions.Length)
        {
            Console.WriteLine(questions[questionNum], 10, 30);
            //Here you store the student answer
            studentAnswer = Console.ReadLine();
            //Here you check the student answer using the same index of answer array
            if (studentAnswer.ToLower() == answers[answerNum].ToLower())
            {
                Console.WriteLine("Correct!");
                questionNum++;
                answerNum++;
                correctAnswer++;
                qcounter++;
            }
            else
            {
                Console.WriteLine("Incorrect.");
                incorrectAnswer.Add(studentAnswer)
                //Remove these increment and the question will be the same in the next loop cycle
                questionNum++; // remove
                answerNum++; // remove
                qcounter++; // remove
            }
            Console.WriteLine();
            Console.WriteLine("Press ENTER for Next question.");
            Console.ReadLine();
            Console.Clear();
        }
public class QuizItem
{
    public string Question { get; set; }
    public string Answer { get; set; }
    public List<string> Choices { get; set; }
    public int CorrectChoiceIndex { get; set; }
    public string UserResponse { get; private set; }
    public bool Result { get; private set; }

    public bool AskQuestion()
    {
        Console.WriteLine(Question);

        for (int i = 0; i < Choices.Count; i++)
        {
            Console.WriteLine($"{i + 1}. {Choices[i]}");
        }

        int choice;
        do
        {
            Console.Write($"Enter response (1 - {Choices.Count}): ");
        } while (!int.TryParse(Console.ReadLine(), out choice) ||
                    choice < 1 || choice > Choices.Count);

        Result = choice - 1 == CorrectChoiceIndex;
        UserResponse = Choices[choice - 1];

        Console.WriteLine(Result ? "Correct!" : "Incorrect.");
        Console.WriteLine();

        return Result;
    }
}
public static List<QuizItem> GetQuizItems()
{
    return new List<QuizItem>
    {
        new QuizItem
        {
            Question = "The fuselage is the center structure of an aircraft and " + 
                "provides the connection for the wings and tail.",
            Choices = new List<string> {"True", "False"},
            CorrectChoiceIndex = 0
        },
        new QuizItem
        {
            Question = "Rolling is the action on the lateral axis of an aircraft.",
            Choices = new List<string> {"True", "False"},
            CorrectChoiceIndex = 0
        },
        new QuizItem
        {
            Question = "Drag is the name of the force that resists movement of an  " + 
                "aircraft through the air.",
            Choices = new List<string> {"True", "False"},
            CorrectChoiceIndex = 0
        },
        new QuizItem
        {
            Question = "Flaps are attached to the trailing edge of a wing structure  " + 
                "and only increases drag.",
            Choices = new List<string> {"True", "False"},
            CorrectChoiceIndex = 1
        },
        new QuizItem
        {
            Question = "Powerplant or engine produces thrust to propel an aircraft.",
            Choices = new List<string> {"True", "False"},
            CorrectChoiceIndex = 0
        },
        new QuizItem
        {
            Question = "Which of the following are part of an aircraft " + 
                "primary flight controls?",
            Choices = new List<string>
                {"Aileron", "Rudder", "Elevators", "All of the above"},
            CorrectChoiceIndex = 3
        },
        new QuizItem
        {
            Question = "The Fuel-air control unit of a reciprocating engine?",
            Choices = new List<string>
                {
                    "Sends fuel to the piston chamber",
                    "Sends air to the piston chamber",
                    "Controls the mixture of air and fuel",
                    "Meters the quantity of fuel"
                },
            CorrectChoiceIndex = 2
        },
        new QuizItem
        {
            Question = "Which of the following is the main source of electrical power " + 
                "when starting an aircraft?",
            Choices = new List<string> 
                {"Primary Bus", "Avionics Bus", "Battery", "GPU (ground power unit)"},
            CorrectChoiceIndex = 2
        },
        new QuizItem
        {
            Question = "The reservoir of a hydraulic system is used for what?",
            Choices = new List<string>
                {
                    "Store and collect fluid from a hydraulic system",
                    "Lubricate components when needed",
                    "Keep the fluid clean",
                    "All of the above"
                },
            CorrectChoiceIndex = 0
        },
        new QuizItem
        {
            Question = "Flying into fog can cause what?",
            Choices = new List<string>
                {
                    "Narrowing of the runway",
                    "An aircraft to stall",
                    "An illusion of pitching up",
                    "A stressful environment for the Pilot and Co-pilot"
                },
            CorrectChoiceIndex = 2
        }
    };
}
private static void Main(string[] cmdArgs)
{
    var quizItems = GetQuizItems();

    foreach (var quizItem in quizItems)
    {
        quizItem.AskQuestion();
    }

    var correctCount = quizItems.Count(item => item.Result);

    Console.WriteLine($"You got {correctCount} out of {quizItems.Count} questions correct!");

    Console.WriteLine("\nLet's review the questions you missed:\n");

    foreach (var quizItem in quizItems.Where(item => !item.Result))
    {
        quizItem.AskQuestion();
    }

    correctCount = quizItems.Count(item => item.Result);
    var percentCorrect = 100.0 * correctCount / quizItems.Count;

    Console.WriteLine($"Your final score was {correctCount} out " +
                        $"of {quizItems.Count}, or {percentCorrect}%!");

    GetKeyFromUser("\nDone! Press any key to exit...");
}
public static void Quiz()
{
    string[] questions = { "The fuselage is the center structure of an aircraft and provides the connection for the wings and tail. \r\nTrue or False?",
    "Rolling is the action on the lateral axis of an aircraft. \r\nTrue or False?",

    "Drag is the name of the force that resists movement of an aircraft through the air. \r\nTrue or False?",

    "Flaps are attached to the trailing edge of a wing structure and only increases drag. \r\nTrue or False?",

    "Powerplant or engine produces thrust to propel an aircraft. \r\nTrue or False?",

    "Which of the following are part of an aircraft primary flight controls? \r\na.Aileron. \r\nb.Rudder. \r\nc.Elevators. \r\nd. All of the above.",
    "The Fuel-air control unit of a reciprocating engine? \r\na.Sends fuel to the piston chamber. \r\nb.Sends air to the piston chamber. \r\nc.Controls the mixture of air and fuel. \r\nd.Meters the quantity of fuel.",

    "Which of the following is the main source of electrical power when starting an aircraft? \r\na.Primary Bus. \r\nb.Avionics Bus. \r\nc.Battery. \r\nd.GPU (ground power unit)",

    "The reservoir of a hydraulic system is used for? \r\na.Store and collect fluid from a hydraulic system. \r\nb.Lubricate components when needed. \r\nc.Keep the fluid clean. \r\nd.All of the above." };
    string[] answers = { "True", "True", "True", "False", "True", "d", "c", "c", "a", "c" };
    string studentAnswer;
    List<int> incorrectAnswers = new List<int>();
    for (int i = 0; i < questions.Length; i++)
    {
        Console.WriteLine(questions[i], 10, 30);
        studentAnswer = Console.ReadLine();
        if (studentAnswer == answers[i])
        {
            Console.WriteLine("Correct!");
        }
        else
        {
            Console.WriteLine("Incorrect.");
            incorrectAnswers.Add(i);
        }
        Console.WriteLine();
        Console.WriteLine("Press ENTER for Next question.");
        Console.ReadLine();
        Console.Clear();
    }
}
public class Quiz
{

    public static void Main()
    {

        Quizard QuizGame = new Quizard();


        QuizGame.Questions.Add(new QuestionObject {
            Question = "The fuselage is the center structure of an aircraft and provides the connection for the wings and tail?",
            Options = new List<string> { "True", "False" },
            CorrectAnswer = "a"
        });

        QuizGame.Questions.Add(new QuestionObject {
            Question = "Rolling is the action on the lateral axis of an aircraft?",
            Options = new List<string> { "True", "False" },
            CorrectAnswer = "a"
        });

        QuizGame.Questions.Add(new QuestionObject {
            Question = "Which of the following are part of an aircraft primary flight controls?",
            Options = new List<string> { "Primary Bus", "Avionics Bus", "Battery", "GPU (ground power unit)" },
            CorrectAnswer = "d"
        });

        QuizGame.Questions.Add(new QuestionObject {
            Question = "The Fuel-air control unit of a reciprocating engine?",
            Options = new List<string> { "Sends fuel to the piston chamber", "Sends air to the piston chamber", "Controls the mixture of air and fuel", "Meters the quantity of fuel" },
            CorrectAnswer = "c"
        });



        Display(QuizGame.Questions);

        while (true) {
            if (QuizGame.Questions.Where(x => x.Correct == false) != null) {
                Console.WriteLine(QuizGame.Questions.Where(x => x.Correct == true).Count() + " out of " + QuizGame.Questions.Count);
                Console.WriteLine("Would you like to try again? Y/N");
                if (Console.ReadLine().ToLower() == "y") {

                    Display(QuizGame.Questions.Where(x => x.Correct == false).ToList());
                } else {
                    break;
                }
            } else {
                break;
            }
        }

        void Display(List<QuestionObject> questions)
        {
            string[] marker = new string[] { "A", "B", "C", "D" };
            foreach (var question in questions) {
                Console.WriteLine(question.Question);
                for (int i = 0; i < question.Options.Count; i++) {
                    Console.WriteLine($"{marker[i]}. {question.Options[i]}");
                }
                Console.Write(">>");
                question.GivenAnswer = Console.ReadLine();

                if (question.GivenAnswer.ToLower() == question.CorrectAnswer.ToLower()) {
                    question.Correct = true;
                } else {
                    question.Correct = false;
                }

                Console.Clear();
            }         
        }
    }        
}


public class QuestionObject{
    public string Question { get; set; }
    public string CorrectAnswer { get; set; }
    public List<string> Options { get; set; }
    public string GivenAnswer { get; set; }
    public bool Correct { get; set; }        
}

public class Quizard
{
    private List<QuestionObject> m_Questions = new List<QuestionObject>();
    public List<QuestionObject> Questions {
        get { return m_Questions; }
        set {
            Questions = value;
        }
    }
    public string UserName { get; set; }
}