在C#中匹配答案

时间:2018-04-09 15:39:37

标签: c#

所以我有这个问题:

  

“1。'男性'这个词意味着什么?\ na。像兔子一样\ n。像个男人\ nc。像狼一样\ n。像马一样\ n”   正如您所看到的,每个选项的一个字符串用\ n分隔。 (a,b,c,d)

所以我写了一个代码来检查它在a,b,c或d

上的答案在哪里
ans = "Like a man";
if (data.Content.StartsWith("a") && data.Content.Contains(ans))
{
    Reply("a");
}
else if (data.Content.StartsWith("b") && data.Content.Contains(ans))
{
    Reply("b");
}
else if (data.Content.StartsWith("c") && data.Content.Contains(ans))
{
    Reply("c");
}
else if (data.Content.StartsWith("d") && data.Content.Contains(ans))
{
    Reply("d");
}

它给我'一个',因为它是答案。我知道为什么,因为我使用Startwith,这是错误的,因为(data.content)从问题开始,因为它是一个字符串。

我的问题是: 我怎么能让程序在我的答案的问题中查找任何匹配,然后写任何a,b,c或d

4 个答案:

答案 0 :(得分:1)

检查这是否对您有所帮助。

<session-config>
<session-timeout>30</session-timeout>
<cookie-config>
<path>/</path>
</cookie-config>
</session-config>

答案 1 :(得分:1)

也许这个LINQ解决方案会有所帮助:

string input = "1. The word 'virile' means what?\na. Like a rabbit\nb. Like a man\nc. Like a wolf\nd. Like a horse\n";
string[] inputSplit = input.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
string ans = "Like a man";
string result = new string(inputSplit.Where(x => x.Contains(ans))
                                     .Select(x => x[0]).ToArray());
Reply(result);

result = b

答案 2 :(得分:0)

public char GetQuestionRef(string question, string answer)
{
    string[] answers = question.Split('\n');
    char question; 

    for(int i = 1; i < answers.Length; i++)
    {
        if(answers[i].Contains(answer))
        {
            question = answers[i].Substring(0, 1);              
        }
    }

    return question;
}

答案 3 :(得分:0)

另一种方法是创建一个包含&#34;测验项&#34;属性的类,例如问题,可能答案列表和正确答案索引。我们还可以让这个类能够提出问题,显示答案,从用户那里得到答案,然后如果答案是正确的,则返回true或false。

例如:

public class QuizItem
{
    public string Question { get; set; }
    public List<string> PossibleAnswers { get; set; }
    public bool DisplayCorrectAnswerImmediately { get; set; }
    public int CorrectAnswerIndex { get; set; }

    public bool AskQuestionAndGetResponse()
    {
        Console.WriteLine(Question + Environment.NewLine);

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

        int response = GetIntFromUser($"\nEnter answer (1 - {PossibleAnswers.Count}): ",
            1, PossibleAnswers.Count);

        if (DisplayCorrectAnswerImmediately)
        {
            if (response == CorrectAnswerIndex + 1)
            {
                Console.WriteLine("\nThat's correct, good job!");
            }
            else
            {
                Console.WriteLine("\nSorry, the correct answer is: {0}",
                    $"{CorrectAnswerIndex + 1}. {PossibleAnswers[CorrectAnswerIndex]}");
            }
        }

        return response == CorrectAnswerIndex + 1;
    }

    // This helper method gets an integer from the user within the specified bounds
    private int GetIntFromUser(string prompt, int min, int max)
    {
        int response;
        do
        {
            Console.Write(prompt);
        } while (!int.TryParse(Console.ReadLine(), out response) ||
                 response < min || response > max);
        return response;
    }
}

现在我们有一个代表QuizItem的类,我们可以创建另一个类来表示Quiz本身。该课程将有一个测验项目列表,能够向用户展示每个项目,保存答案,并显示测验结果。

例如:

public class Quiz
{
    public Quiz(User user)
    {
        if (user == null) throw new ArgumentNullException(nameof(user));
        User = user;
        QuizItems = new List<QuizItem>();
    }

    public List<QuizItem> QuizItems { get; set; }
    public User User { get; set; }
    public DateTime StartTime { get; private set; }
    public DateTime EndTime { get; private set; }

    public void BeginTest()
    {
        Console.Clear();

        if (QuizItems == null)
        {
            Console.WriteLine("There are no quiz items available at this time.");
            return;
        }

        Console.WriteLine($"Welcome, {User.Name}! Your quiz will begin when you press a key.");
        Console.WriteLine($"There are {QuizItems.Count} multiple choice questions. Good luck!\n");
        Console.Write("Press any key to begin (or 'Q' to quit)...");
        if (Console.ReadKey().Key == ConsoleKey.Q) return;

        Console.SetCursorPosition(0, Console.CursorTop);
        Console.Write(new string(' ', Console.WindowWidth));
        Console.SetCursorPosition(0, Console.CursorTop - 1);

        var itemNumber = 1;
        StartTime = DateTime.Now;

        foreach (var item in QuizItems)
        {
            Console.WriteLine($"Question #{itemNumber}");
            Console.WriteLine("-------------");

            itemNumber++;
            var result = item.AskQuestionAndGetResponse();
            User.QuestionsAnswered++;
            if (result) User.CorrectAnswers++;
        }

        EndTime = DateTime.Now;
        var quizTime = EndTime - StartTime;

        Console.WriteLine("\nThat was the last question. You completed the quiz in {0}",
            $"{quizTime.Minutes} minues, {quizTime.Seconds} seconds.");

        Console.WriteLine("\nYou answered {0} out of {1} questions correctly, for a grade of '{2}'",
            User.CorrectAnswers, User.QuestionsAnswered, User.LetterGrade);
    }
}

该类使用User类,用于存储用户名及其结果:

public class User
{
    public User(string name)
    {
        Name = name;
    }

    public string Name { get; set; }
    public int QuestionsAnswered { get; set; }
    public int CorrectAnswers { get; set; }
    public string LetterGrade => GetLetterGrade();

    private string GetLetterGrade()
    {
        if (QuestionsAnswered == 0) return "N/A";

        var percent = CorrectAnswers / QuestionsAnswered * 100;

        if (percent < 60) return "F";
        if (percent < 63) return "D-";
        if (percent < 67) return "D";
        if (percent < 70) return "D+";
        if (percent < 73) return "C-";
        if (percent < 77) return "C";
        if (percent < 80) return "C+";
        if (percent < 83) return "B-";
        if (percent < 87) return "B";
        if (percent < 90) return "B+";
        if (percent < 90) return "A-";
        if (percent < 97) return "A";
        return "A+";
    }
}

这似乎做了很多工作,但如果您要提出很多问题,最终会通过减少重复代码并在单独的类和方法中封装功能来节省时间。添加新功能也会更容易。

要使用这些类,我们只需实例化一个新的Quiz类,将其传递给User,填充QuizItems,然后调用BeginTest

private static void Main()
{
    Console.Write("Please enter your name: ");
    var userName = Console.ReadLine();

    var quiz = new Quiz(new User(userName));
    PopulateQuizItems(quiz);          
    quiz.BeginTest();

    GetKeyFromUser("\nDone!! Press any key to exit...");
}

我将代码放在一个单独的方法中填充测验项目,以减少Main方法中的混乱。现在它只有你的一个问题:

private static void PopulateQuizItems(Quiz quiz)
{
    if (quiz == null) return;

    quiz.QuizItems.Add(new QuizItem
    {
        Question = "What does the word 'virile' mean?",
        PossibleAnswers = new List<string>
        {
            "Like a rabbit",
            "Like a man",
            "Like a wolf",
            "Like a horse"
        },
        CorrectAnswerIndex = 1,
        DisplayCorrectAnswerImmediately = true
    });
}

然后它看起来像这样:

enter image description here