繁体   English   中英

在 c# 上需要帮助

[英]Need help on c#

我最近开始学习 c# 并且我试图做一些类似琐事的事情,我已经创建了 3 个封闭式问题(答案为 0-3),现在我试图创建一个开放式问题,但我不知道如何让它开放,如何让用户输入答案以及如何检查其真假。 任何帮助都会很棒!

这是我的代码:

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;
        }

    }
}

非常感谢您的帮助!

一个简单的第一种方法是在答案中声明预期的关键字,并检查用户答案是否包含任何/部分/所有预期的关键字。

新类型的问题必须像这样创建:

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

像这样打开 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;
    }
}

检查关键字而不是此处使用的“包含”的好方法是模糊搜索,但这不是启动 c# imo 的最佳方法,因为它可能不容易/有趣。

如果您对此有任何疑问,请告诉我(我尽量不更改太多代码以免造成混淆)。

Ps:将新类型的问题集成到您的列表中的一个好方法是声明一个接口。 https://docs.microsoft.com/en-gb/dotnet/csharp/language-reference/keywords/interface (它基本上是一种声明实现接口的 object 必须具有的方法/属性的方法)。

用法:

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