简体   繁体   English

在 c# 上需要帮助

[英]Need help on c#

I have recently started to learn c# and Im trying to make something like trivia, i have created 3 closed questions(with answers 0-3), and now Im trying to create an open question but i don't know how to make it open, how to make the user enter an answer and how to check if its true or false.我最近开始学习 c# 并且我试图做一些类似琐事的事情,我已经创建了 3 个封闭式问题(答案为 0-3),现在我试图创建一个开放式问题,但我不知道如何让它开放,如何让用户输入答案以及如何检查其真假。 any help will be greatfull!任何帮助都会很棒!

this is my code:这是我的代码:

using FirstProject;
using System;
using System.Collections.Generic;

namespace FirstProject{
    class Program{
        static void Main(string[] args){
            String[] q1a = { "Blue", "Black", "Red", "Green" };
            QuestionA q1 = new QuestionA("What is the color of a tomato?",q1a,2);

            String[] q2a = { "10", "25", "125", "2.5" };
            QuestionA q2 = new QuestionA("how much is 5X5", q2a, 1);

            String[] q3a = { "2", "5", "4", "10" };
            QuestionA q3 = new QuestionA("how many eyes do i have?", q3a, 0);

            List<QuestionA> QL = new List<QuestionA>() { q1, q2, q3, q4 };
            int CorrectA = 0;
            int TotalQ = QL.Count;
            Random Rnd = new Random();
            while (QL.Count > 0)
            {
                int RM = Rnd.Next(0, QL.Count);
                Boolean Answer = QL[RM].PrintQuestion();
                if (Answer) CorrectA++;
                QL.RemoveAt(RM);

            }
            //Console.WriteLine(TotalQ);
            Double GrD = ((Double)CorrectA / (Double)TotalQ) * 100;
            if (GrD < 56)
                Console.WriteLine("You Have failed, Your Grade is:" + GrD);
            else 
                Console.WriteLine("Congrats u Passed the test with a grade of:" + GrD);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace FirstProject
{
    class QuestionA
    {
        public String question;
        public String[] answers;
        public int correct;
        public QuestionA(String question, String[] answers, int correct)
        {
            this.question = question;
            this.answers = answers;
            this.correct = correct;
        }
        public Boolean PrintQuestion()
        {
            Console.WriteLine("Plz type the right answer(0-3)");
            Console.WriteLine("Q:" + this.question);
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine(i + ":" + this.answers[i]);
            }
            return this.CheckAnswer();
        }
        public Boolean CheckAnswer()
        {
            int answer = Convert.ToInt32(Console.ReadLine());
            if (this.correct == answer)
            {
                return true;
            }
            return false;
        }

    }
}

Thx a lot with any help!非常感谢您的帮助!

A simple first approach would be to declare keywords expected in the answer and to check if the user answer contains any/some/all of the expected keywords.一个简单的第一种方法是在答案中声明预期的关键字,并检查用户答案是否包含任何/部分/所有预期的关键字。

The new type of question would have to created like this:新类型的问题必须像这样创建:

String[] q4a = { "black" };
QuestionOpen q4 = new QuestionOpen("what is the color of the sky's at night?", q4a);

with the QuestionOpen like this:像这样打开 QuestionOpen:

class QuestionOpen
{
    public String question;
    public String[] correctKeyWords;
    public QuestionOpen(String question, String[] correctKeyWords)
    {
        this.question = question;
        this.correctKeyWords = correctKeyWords;
    }
    public Boolean PrintQuestion()
    {
        Console.WriteLine("Plz type your answer");
        Console.WriteLine("Q:" + this.question);
        return this.CheckAnswer();
    }
    public Boolean CheckAnswer()
    {
        string answer = Console.ReadLine();
        bool isAnswerCorrect = false;
        foreach( string keyWord in this.correctKeyWords)
        {
            if (answer.Contains(keyWord))
            {
                isAnswerCorrect = true;
                //You could have a counter and check if a percentage of the keyword are in the answer
            }
            else
            {
                //Nothing do do
            }
        }
        return isAnswerCorrect;
    }
}

A good way to check the keywords instead of the "contains" used here would be a fuzzy search but it isn't the best way to start c# imo as it might not be easy/fun to do.检查关键字而不是此处使用的“包含”的好方法是模糊搜索,但这不是启动 c# imo 的最佳方法,因为它可能不容易/有趣。

Let me know if you have any question regarding this (I tried not to change to much code as to not make it confusing).如果您对此有任何疑问,请告诉我(我尽量不更改太多代码以免造成混淆)。

Ps: A nice way to integrate the new type of question into your list would be to declare an interface. Ps:将新类型的问题集成到您的列表中的一个好方法是声明一个接口。 https://docs.microsoft.com/en-gb/dotnet/csharp/language-reference/keywords/interface (It's basically a way to declare the methods/property that the object implementing the interface must have). https://docs.microsoft.com/en-gb/dotnet/csharp/language-reference/keywords/interface (它基本上是一种声明实现接口的 object 必须具有的方法/属性的方法)。

usage:用法:

List<IQuestion> QL = new List<IQuestion>() { q1, q2, q3, q4};
        int CorrectA = 0;
        int TotalQ = QL.Count;
        Random Rnd = new Random();
        while (QL.Count > 0)
        {
            int RM = Rnd.Next(0, QL.Count);
            Boolean Answer = QL[RM].PrintQuestion();
            if (Answer) CorrectA++;
            QL.RemoveAt(RM);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM