我需要此测验来存储用户错误回答的问题,然后在测验结束时,程序将为用户提供选择以查看错误回答的问题并再次执行。有人可以向我解释如何做或为我指出正确的方向吗?
先谢谢您
这是我到目前为止所拥有的。
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);
}
}
}
答案 0 :(得分:3)
这是另一种方法,该方法是创建一个代表问题的类(我也叫QuizItem
,因为它也有答案),并且它具有代表问题的属性,可能的答案列表,正确答案的索引;并具有提出问题并获得(并存储)用户回复的能力。
这似乎需要更多的准备工作,但是最后可以很轻松地询问/回答和呈现结果数据:
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...");
}
答案 1 :(得分:2)
步骤:
这是代码中要编辑的部分(它将存储所有答案)
//Declare the array with answer
string[] answers = { "True", "True", "True", "False", "True", "d", "c", "c", "a", "c" };
string[] studentAnswer = new string[answers.Length];
//Use the array in you while loop
while (questionNum < questions.Length)
{
Console.WriteLine(questions[questionNum], 10, 30);
//Here you store the student answer
studentAnswer[answerNum] = Console.ReadLine();
//Here you check the student answer using the same index of answer array
if (studentAnswer[answerNum] == answers[answerNum])
{
Console.WriteLine("Correct!");
questionNum++;
answerNum++;
correctAnswer++;
qcounter++;
}
else
{
Console.WriteLine("Incorrect.");
//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();
}
编辑后的内容:
最好使用.ToLower()
函数,以便将字符转换为小写,并且学生可以同时键入(小写和大写)而不会得到错误的结果。
为此,您必须编辑if行:
if (studentAnswer[answerNum].ToLower() == answers[answerNum].ToLower())
在PAPARAZZO评论后编辑:
您在这里存储了所有错误的答案:
//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();
}
答案 2 :(得分:0)
只需使用列表即可。
您应该测试响应以获取有效输入。如果他们输入f怎么办?
这是一种更直接的方法。
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();
}
}
答案 3 :(得分:0)
我也很无聊:
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; }
}
没有添加所有问题,也没有进行任何边界检查,因此,如果您有4个以上的问题,除非您添加到marker
数组中,否则问题就会完全消失