【问题标题】:How can I offer A-D in an array as the correct answer, as opposed to 0-3?我如何在数组中提供 A-D 作为正确答案,而不是 0-3?
【发布时间】:2019-05-01 06:19:52
【问题描述】:

我刚刚开始使用 C# 进行编程,并且在某个特定部分上非常挣扎。我整理了一个多项选择测验,并在一个数组中列出了参数。我遇到的麻烦是如何生成字母 A-D 作为正确答案,而不是 0、1、2、3。任何人都可以帮忙吗?我觉得这很简单,但我就是想不通。非常感谢任何帮助,谢谢!

我也将答案设置为整数 '0,1,2,3' 但现在它甚至没有将它们连接到正确的答案:(

我有一个“问题”类,其中包含:

public Question(string q, string[] answersList, string typeOfQuestion, int correctAnswer)

然后我用

在程序中创建问题对象
new Question("What colour is the sky?", 
    new string[] { "A. Pink", "B. Blue", "C. Purple", "D.Yellow" },
    Question.multipleChoice, B );

【问题讨论】:

  • 展示你的尝试
  • 你如何生成选项 0,1,2,3
  • 在 ASCII(因此是不可移植的 C)中,你会写 'A' + n 其中n 是数字。 C# 坚持使用 ASCII(尽管字符串更复杂),因此需要检查此语法是否有效。 (我不是 C# 程序员,所以不能多说)。
  • 所以我为“问题”创建了一个类,其中“public Question(string q, string[] answersList, string typeOfQuestion, int CorrectAnswer)”是数组。在程序中,我一直在关注这个
  • 将您的代码与@NewProgrammer 的问题一起发布

标签: c# arrays class object indexing


【解决方案1】:

这是一个小控制台应用程序,它为数组中的项目生成字母,并接受用户输入(大写或小写)并将其转换回数字。

关键是 'a' 是 65 而 'A' 在 ASCII 中是 97,并且您可以使用 Convert.ToChar(i) 将数字转换为字母。控制台提供 ASCII 值,但您可以找到 ASCII 值和带有(int)Char.GetNumericValue('a');的字母

class Program
{
    static void Main(string[] args)
    {
        var q1 = new Question("What colour is the sky?", new string[] { "Pink", "Blue", "Purple", "Yellow" }, "multipleChoice", 1);

        AskQuestion(q1);
    }

    private static void AskQuestion(Question q)
    {
        Console.WriteLine(q.Prompt);
        for (var i = 0; i < q.AnswersList.Length; i++)
        {
            Console.WriteLine($"{Convert.ToChar(65 + i)} - {q.AnswersList[i]}");
        }

        var answer = Console.ReadKey(true);

        if (answer.KeyChar == 65 + q.CorrectAnswer ||
            answer.KeyChar == 97 + q.CorrectAnswer)
        {
            Console.WriteLine("Correct");
        }
        else
        {
            Console.WriteLine("Wrong");
        }
    }

    private class Question
    {
        public string Prompt { get; }
        public string[] AnswersList { get;  }
        public string TypeOfQuestion { get; }
        public int CorrectAnswer { get;  }

        public Question(string prompt, string[] answersList, string typeOfQuestion, int correctAnswer)
        {
            this.Prompt = prompt;
            this.AnswersList = answersList;
            this.TypeOfQuestion = typeOfQuestion;
            this.CorrectAnswer = correctAnswer;
        }
    }
}

【讨论】:

  • Nitpick:.Net 不使用“ASCII”,而是使用 Unicode(编码为 UTF-16)。但是 ASCII(0-127)中的字符代码在 Unicode 中确实具有相同的含义。
【解决方案2】:

你需要更新类结构,这样就容易管理了

 public class Question
{
    public string q { get; set; }
    public List<answersList> answersList { get; set; }
    public string typeOfQuestion { get; set; }
    public int CorrectAnswer { get; set; }
}
public class answersList
{
    public string answer { get; set; }
    public string code { get; set; }
}

用于制作对象

 var objQuestion = new Question
        {
            q = "What colour is the sky?",
            CorrectAnswer = 1,
            typeOfQuestion = "5",
            answersList=new List<answersList> { new answersList { answer="xyz", code="a"} }
        };

【讨论】:

  • 我不认为 List 在这里真的有帮助。当您需要添加和删除项目时,它们非常有用,尤其是在中间,但在此示例中,列表没有提供您无法使用数组执行的任何操作。创建后长度不变,按位置访问item。
猜你喜欢
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多