gpt4 book ai didi

c# - 我如何添加代码以使用户无法输入未列出的答案? C#

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:44 25 4
gpt4 key购买 nike

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

namespace Kenneth_ForquerENG_115
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Kenneth Forquer ENG 115");

这是我存储问题和答案的地方

        List<Question> question = new List<Question>
{


new Question("What stage bleed air is used to start the engine?", new string[] { "8th", "9th", "1st", "13th" }, Question.multipleChoice, 2),
new Question("Igniters spark to combust the fuel.", new string[] { "true", "false" }, Question.trueAndFalse, 0),
new Question("What is the 1st stage in the cycle of the Gas turbine?", new string[] { "Intake", "Exhaust", "Power", "Combustion" }, Question.multipleChoice, 0),
new Question("What is the 3rd stage in the cycle of the Gas turbine?", new string[] { "Compression", "Combustion", "Intake", "Exhaust" }, Question.multipleChoice, 1),
new Question(" What is the last stage in the cycle of the Gas turbine?", new string[] { "Compression", "Power", "Intake", "Exhaust" }, Question.multipleChoice, 3),
new Question("Ngg refers to speed.", new string[] { "true", "false" }, Question.trueAndFalse, 0),
new Question("Npt refers to torque", new string[] { "true", "false" }, Question.trueAndFalse, 1),
new Question("What are the LM2500 metering points?", new string[] { "3", "2", "5.4", "All of the above" }, Question.multipleChoice, 3),
new Question("Which of these are a component of the compressor?", new string[] { "Inlet Plenum", "Gearbox", "Stator", "All of the above" }, Question.multipleChoice, 2),
new Question("What company manufactures the LM2500?", new string[] { "GM", "Ford", "Toyota", "Rolls Royce" }, Question.multipleChoice, 3)
};


for (int i = 0; i < question.Count; i++)
{
Console.WriteLine("--------------------------------");
Console.WriteLine("Question #" + (1 + i).ToString());
if (question[i].Ask())
{
Results.AddResult(question[i]);
Console.WriteLine("Press any key to continue to the next question.");
//Console.WriteLine("--------------------------------");
}
else
{
Results.AddResult(question[i]);
}
Console.Clear();
}

Console.WriteLine("--------------------------------");
Console.WriteLine("End of the first attempt.");
int tempScore = 0;

for (int i = 0; i < Results.firstResults.Count; i++)
{
if (Results.firstResults[i].isCorrect)
{
tempScore++;
}
}
Console.WriteLine("Your current mark is: " + tempScore + "/" + Results.firstResults.Count.ToString());
Console.WriteLine("--------------------------------");
Console.WriteLine("Press any key to continue...");
Console.ReadKey();

Results.RunSecondAttemp();

Console.WriteLine("--------------------------------");
Console.WriteLine("End of the Quiz!");

int tempFinalScore = 0;

for (int i = 0; i < Results.finalResults.Count; i++)
{
if (Results.finalResults[i].isCorrect)
{
tempFinalScore++;
}
}

Console.WriteLine("Your final mark is: ");
Console.WriteLine(tempFinalScore + "/" + Results.finalResults.Count.ToString());
if (tempFinalScore > 5)
{
Console.WriteLine("Good Job!");
}
else
{
Console.WriteLine("Better luck next time...");
}
Console.WriteLine("--------------------------------");
Console.WriteLine("Press any key to exit the console...");
Console.ReadKey();
}

}
}

namespace Kenneth_ForquerENG_115
{
class Question
{
public string question;
public string[] answers;
public bool isCorrect;
public string inputAnswer;
private int correctIndex;
private string questionType;
public static string trueAndFalse = "TF";
public static string multipleChoice = "MC";

此处是问题的输入位置。

    public Question(string q, string[] answersList, string typeOfQuestion, int correctAnswer)
{
question = q;
questionType = typeOfQuestion;
if (questionType == multipleChoice)
answers = new string[4];
else if (questionType == trueAndFalse)
answers = new string[2];

for (int i = 0; i < answersList.Length; i++)
{
this.answers[i] = answersList[i];
}

correctIndex = correctAnswer;
}


public bool Ask()
{
Console.WriteLine(question);
if (questionType == multipleChoice)
{
Console.WriteLine("Input an answer from the following possibilities...");
}
else
{
Console.WriteLine("Please input 'true' or 'false'... ");
}

for (int i = 0; i < answers.Length; i++)
{
Console.WriteLine(answers[i]);
}

Console.WriteLine("--------------------------------");
inputAnswer = Console.ReadLine();


if (inputAnswer == answers[correctIndex])
{
Console.Clear();
Console.WriteLine("Correct!");
isCorrect = true;
Console.WriteLine("Press any key to continue to the next question.");
Console.ReadKey();
}
else
{
Console.Clear();
Console.WriteLine("Incorrect.");
isCorrect = false;
Console.WriteLine("Press any key to continue to the next question.");
Console.ReadKey();
}

return isCorrect;
}

public void PrintQuestion()
{
Console.WriteLine(question);
if (questionType == multipleChoice)
{
Console.WriteLine("Input an answer from the following possibilities...");
}
else
{
Console.WriteLine("Please input 'true' or 'false'... ");
}

for (int i = 0; i < answers.Length; i++)
{
Console.WriteLine(answers[i]);
}
}
}
}

namespace Kenneth_ForquerENG_115
{
static class Results
{
public static List<Question> firstResults = new List<Question>();
public static List<Question> finalResults = new List<Question>();


public static void AddResult(Question questionResult)
{
firstResults.Add(questionResult);
}

public static void AddFinalResult(Question question)
{
finalResults.Add(question);
}

public static void RunSecondAttemp()
{
Console.Clear();
Console.WriteLine("Attempt #2:");

for (int i = 0; i < firstResults.Count; i++)
{
Console.WriteLine("\n--------------------------------");
Console.WriteLine("\nQuestion " + (1 + i).ToString());

if (firstResults[i].isCorrect)
{
Console.WriteLine("\nThis one was correct!");
firstResults[i].PrintQuestion();
Console.WriteLine("Your answer: " + firstResults[i].inputAnswer);
AddFinalResult(firstResults[i]);
Console.WriteLine("Press any key to continue to the next question.");
Console.ReadKey();
}
else
{
Console.WriteLine("This one was wrong on the first attempt! Please try again.");
if (firstResults[i].Ask())
{
AddFinalResult(firstResults[i]);
Console.WriteLine("Press any key to continue to the next question.");
}
else
{
AddFinalResult(firstResults[i]);
}

Console.WriteLine("\n--------------------------------");
}

Console.Clear();
}
}
}
}

基本上我想做的是让用户只能输入问题的答案。如果他们输入未列出的内容,它将显示一条错误消息,例如“输入不正确,请从给出的答案中选择”。因此,如果他们键入未列出的内容,则会出现该消息,然后返回到问题,直到他们键入选项。

最佳答案

一种选择是在检查答案是否正确之前检查他们的答案是否在答案选项中。你可以这样做:

while (Array.IndexOf(answers, inputAnswer) < 0)
{
Console.WriteLine("incorrect input, please choose from given answers.");
inputAnswer = Console.ReadLine();
}

Array.IndexOf将返回 -1如果inputAnswer不在 answers 中大批。 while在用户输入 answers 中的答案之前,循环不会中断。数组,和 inputAnswer将包含 answers 中的答案当循环中断时。

这将放在 Ask 中包含

的行之前的方法
if (inputAnswer == answers[correctIndex])

关于c# - 我如何添加代码以使用户无法输入未列出的答案? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47426777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com