【问题标题】:How to call codes randomly? [duplicate]如何随机调用代码? [复制]
【发布时间】:2019-06-01 02:46:29
【问题描述】:

我是 C# 编程的新手,正在尝试在控制台中编写类似 Millionare 的知识竞赛游戏。

我正在尝试以一种方法添加所有问题,并且我想随机回答这些问题。这个怎么做?我不是在等待确切的代码或其他东西,我不知道该使用什么或我可以使用哪种算法,无论如何,谢谢。

有我在main方法中编写的代码,在方法中“问题”将是所有问题,我想随机调用它们。

static void Main(string[] args)
    {
        Console.ForegroundColor = ConsoleColor.DarkGreen; 

        Console.WriteLine("----------> Welcome to Knowledge Competition! <----------"); 
        Console.WriteLine(); 

        Console.WriteLine("Press E/e for start the competition : "); 
        char answer = char.Parse(Console.ReadLine());
        if (answer == 'e' || answer == 'E')
        {
            Console.Clear();

        }
        else
        {
            Console.WriteLine("Entered wrong letter.");
        }

        Console.ReadLine();
    }
    static void Competition()
    {

    }
    static void EasyQuestions()
    {


    }
    static void NormalQuestions()
    {

    }
    static void HardQuestions()
    {

    }

【问题讨论】:

  • google 为c# random choice 向您展示了什么,您似乎没有随机包含任何内容?你知道Listsystring&gt;吗?以及如何从中得到一个?你研究过吗?询问“请帮助我”的问题往往是寻求高度本地化的指导,或者在某些情况下,寻求持续或私人帮助,这不适合我们的问答形式。它也相当模糊,最好用更具体的问题代替。请阅读Why is “Can someone help me?” not an actual question?
  • 我们的目标是为特定问题建立一个问答库……您没有显示特定问题,而是"I dont know how to do x - this is the code I did (which does nothing with X" 问题。给我一秒钟 - 我会给你一些关于随机的 SO 答案,这样你就可以阅读它......
  • 您可能想重读How to Ask - 首先要做的是搜索 - 有非常有可能别人写了一个问题,你可以简单地阅读答案的 。我通过谷歌搜索c# random question game btw 得到了这 4 个骗子。 google 比 SO 本身更擅长搜索-
  • 酷 - 然后也许 edit 并解释他们为什么不解决问题:)

标签: c#


【解决方案1】:

我没有测试过,但应该是这样的:

Random rnd = new Random();
List<string> randomMethod = new List<string> {"Competition","NormalQuestions","Etc.." }
MethodInfo rndMethod= this.GetType().GetMethod(randomMethod[rnd.Next(1,randomMethod,Length)] );
rndMethod.Invoke(this, null);

【讨论】:

    猜你喜欢
    • 2011-07-02
    • 2012-09-17
    • 1970-01-01
    • 2012-07-28
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多