如何在C#中拥有n个变量

发布于 2024-10-08 17:28:30 字数 456 浏览 0 评论 0原文

想象一下这段代码

for (int iDay = 1; iDay <= total_days; iDay++)
{

    question = CheckString(s.challenge_1_q);
    answer = CheckStringA(s.challenge_1_a);

    // more here
}

,但我真正拥有的是从 challenge_1_qchallenge_24_qchallenge_1_achallenge_24_a

我最好的选择是什么拥有动态变量,因为今天是 24,“明天”可能只有 18。

使用 dynamic 是正确的方法吗?或者我真的需要一个开关并忘记动态性?

imagine this code

for (int iDay = 1; iDay <= total_days; iDay++)
{

    question = CheckString(s.challenge_1_q);
    answer = CheckStringA(s.challenge_1_a);

    // more here
}

but what I really have is from challenge_1_q to challenge_24_q and challenge_1_a to challenge_24_a

what is my best option to have dynamic variables as today it's 24, "tomorrow" could be only 18.

is the use of dynamic the proper way? or I really need to have a switch and forget about dynamism ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅气尐潴 2024-10-15 17:28:30

创建一个名为 QuestionAnswer 的类,然后在 s 上存储一个 List。
访问代码将如下所示:

question = CheckString(s.QuestionAnswers[i].Question);
answer = CheckStringA(s.QuestionAnswers[i].Answer);

QuestionAnswer 类:

public class QuestionAnswer
{
  public string Question{get; set;}
  public string Answer{get; set;}
}

以及现有类的定义:

public List<QuestionAnswer> QuestionAnswers = new List<QuestionAnswer>();

您可以将数十个项目添加到列表中,而不是使用数十个变量:

QuestionAnswer qa = new QuestionAnswer();
qa.Question = "What letter comes after A?";
qa.Answer = "B";
QuestionAnswers.Add(qa);
//repeat for all your questions.

Create a class called QuestionAnswer, then store a List on s.
The accessing code will look like this:

question = CheckString(s.QuestionAnswers[i].Question);
answer = CheckStringA(s.QuestionAnswers[i].Answer);

The QuestionAnswer class:

public class QuestionAnswer
{
  public string Question{get; set;}
  public string Answer{get; set;}
}

And the definition on your existing class:

public List<QuestionAnswer> QuestionAnswers = new List<QuestionAnswer>();

Instead of having dozens of variables, you add dozens of items to the list:

QuestionAnswer qa = new QuestionAnswer();
qa.Question = "What letter comes after A?";
qa.Answer = "B";
QuestionAnswers.Add(qa);
//repeat for all your questions.
海夕 2024-10-15 17:28:30

您还可以创建 Structure

struct QuestionBank
{
   public string Question;
   public string Answer;            
}

并使用类似的东西

 QuestionBank []Quiz = new QuestionBank[n]; //n is number of question and answers
 Quiz[0].Question = "SomeQuestion";
 Quiz[0].Answer = "SomeAnswer";

 Quiz[1].Question = "SomeAnotherQuestion";
 Quiz[1].Answer = "AnswerAsWell";

 .....

 Quiz[n].Question = "nth Question";
 Quiz[n].Answer = "nth AnswerAsWell";

You can also create Structure

struct QuestionBank
{
   public string Question;
   public string Answer;            
}

And use Something like this

 QuestionBank []Quiz = new QuestionBank[n]; //n is number of question and answers
 Quiz[0].Question = "SomeQuestion";
 Quiz[0].Answer = "SomeAnswer";

 Quiz[1].Question = "SomeAnotherQuestion";
 Quiz[1].Answer = "AnswerAsWell";

 .....

 Quiz[n].Question = "nth Question";
 Quiz[n].Answer = "nth AnswerAsWell";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文