Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使方法从类变为数组?_C#_Arrays_Methods - Fatal编程技术网

C# 如何使方法从类变为数组?

C# 如何使方法从类变为数组?,c#,arrays,methods,C#,Arrays,Methods,目前我正在C控制台应用程序中制作一个问答游戏。我还把测验题放在课堂上每种方法中。然而,我有随机测验,因此当用户再次玩游戏时,他们不会遇到与之前相同的测验顺序。我在这里的问题是如何在数组中的一个类中创建一个方法来连接另一个类?我想,当我在一个测验类测验中调用一个方法时,它是随机的 class Quiz { public static void Quiz1() { Program.DefaultMethod(2, 3); //This method from Prog

目前我正在C控制台应用程序中制作一个问答游戏。我还把测验题放在课堂上每种方法中。然而,我有随机测验,因此当用户再次玩游戏时,他们不会遇到与之前相同的测验顺序。我在这里的问题是如何在数组中的一个类中创建一个方法来连接另一个类?我想,当我在一个测验类测验中调用一个方法时,它是随机的

class Quiz
{
    public static void Quiz1()
    {
        Program.DefaultMethod(2, 3); //This method from Program class

        Console.WriteLine("What is this?");
        Console.WriteLine("│ .. / .- -- / .--. .-. --- --. .-. .- -- -- . .-.");
        Console.WriteLine("│ (hint: sound)");

        Text.Answer(2, 7, 2, 8, "I AM PROGRAMMER", "I am programmer", "i am programmer", 3); //This method from Text class  
    }

    public static void Quiz2()
    {
        Program.DefaultMethod(2, 3); //This method from Program class

        Console.WriteLine("2 - 4 - 12 - 44 - ?");

        Text.Answer(2, 6, 2, 7, "172", "172", "172", 3); //This method from Text class
    }

    public static void Quiz3()
    {
        Program.DefaultMethod(2, 3); //This method from Program class

        Console.WriteLine("If MACHINE is LBBIHOD");
        Console.WriteLine("│ So PROGRAM is....");

        Text.Answer(2, 7, 2, 8, "OSNHQBL", "osnhqbl", "Osnhqbl", 3); //This method from Text class
    }
    //Sorry I just copy 3 question. Because I have so many question XD
}
从这个类中,我想调用来自测验类的方法

class Text
{
    public static void Word(string Teks, int x, int y, ConsoleColor wt, ConsoleColor wb)
    {
        Console.SetCursorPosition(x, y);
        Console.ForegroundColor = wt;
        Console.BackgroundColor = wb;
        Console.Write(Teks);
    }

    public static void Answer(int x1, int y1, int x2, int y2, string correctanswer1, string correctanswer2, string correctanswer3, int count)
    {
        //Quiz[] kuis = {Quiz1(), Quiz2(), Quiz3()}
        //I TRY TO MAKE LIKE ABOVE BUT MAYBE IT'S NOT LIKE THAT

        Console.SetCursorPosition(x1, y1);
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Your answer : ");

        string answer;
        int chance = 0;
        do
        {
            Console.SetCursorPosition(x2, y2);
            Console.WriteLine("                                                                          ");
            Console.SetCursorPosition(x2, y2);
            answer = Console.ReadLine();
            chance++;
            if (answer != correctanswer1 || answer != correctanswer2 || answer != correctanswer3)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.SetCursorPosition(x2, y2 + 1);
                Console.WriteLine("Sorry, your answer is incorrect...");
                Console.Beep();
                Console.ForegroundColor = ConsoleColor.White;
                Console.ReadKey(true);
                Console.SetCursorPosition(x2, y2 + 1);
                Console.WriteLine("                                   ");
            }
        } while (chance < count && answer != correctanswer1 || answer != correctanswer2 || answer != correctanswer3);

        if (answer == correctanswer1 || answer != correctanswer2 || answer != correctanswer3)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.SetCursorPosition(x2, y2 + 1);
            Console.WriteLine("Congratulation! Your answer is correct...");
            Console.ForegroundColor = ConsoleColor.White;
            Console.ReadKey(true);
            //SO, FROM THIS LINE I WANNA CALL 1 METHOD FROM QUIZ CLASS RANDOMLY
        }
        else
        {
            GameOver();
        }
    }

谢谢你的帮助

使用类似foreach或for的迭代器来构建数组;然而,在您的情况下,它看起来不像您正在存储值;而是设置文本框的值,这样您就可以调用返回空值的方法。

以下只是为了让您了解它的工作原理,我使用了一个操作代理列表,并随机调用了它们

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        Quiz quiz = new Quiz();
        quiz.Quiz1();
    }
}

class Quiz
{

    List<Action> methods = new List<Action>();//List of action delegates
    Random random = new Random();//Random instance

    public Quiz()
    {
       //Populate the List with your methods
        methods.Add(()=>Quiz1());
        methods.Add(()=>Quiz2());
        methods.Add(()=>Quiz3());
    }

    public void Quiz1() //Removed Static
    {
       // Program.DefaultMethod(2, 3); //This method from Program class

        Console.WriteLine("What is this?");
        Console.WriteLine("│ .. / .- -- / .--. .-. --- --. .-. .- -- -- . .-.");
        Console.WriteLine("│ (hint: sound)");

        if (Text.Answer(2, 7, 2, 8, "I AM PROGRAMMER", "I am programmer", "i am programmer", 3)) //This method from Text class  
        {
            int r = random.Next(methods.Count());//Get a random number
            methods[r]();//Call the method using the random number
        }
        else
       {
       GameOver();
       }
    }

    public void Quiz2()
    {
      //  Program.DefaultMethod(2, 3); //This method from Program class

        Console.WriteLine("2 - 4 - 12 - 44 - ?");

        if (Text.Answer(2, 6, 2, 7, "172", "172", "172", 3)) //This method from Text class
        {
            int r = random.Next(methods.Count());//Get a random number
            methods[r]();//Call the method using the random number
        }
   else
  {
  GameOver
  }
    }

    public void Quiz3()
    {
      //  Program.DefaultMethod(2, 3); //This method from Program class

        Console.WriteLine("If MACHINE is LBBIHOD");
        Console.WriteLine("│ So PROGRAM is....");

        if (Text.Answer(2, 7, 2, 8, "OSNHQBL", "osnhqbl", "Osnhqbl", 3)) //This method from Text class
        {
            int r = random.Next(methods.Count());
            methods[r]();

        }
    }
    //Sorry I just copy 3 question. Because I have so many question XD
}

class Text
{


    public static void Word(string Teks, int x, int y, ConsoleColor wt, ConsoleColor wb)
    {
        Console.SetCursorPosition(x, y);
        Console.ForegroundColor = wt;
        Console.BackgroundColor = wb;
        Console.Write(Teks);
    }
    //Now your Answer method returns a bool indicating if the user will continue answering more questions or is it GameOver
    public static bool Answer(int x1, int y1, int x2, int y2, string correctanswer1, string correctanswer2, string correctanswer3, int count)
    {
        //Quiz[] kuis = {Quiz1(), Quiz2(), Quiz3()}
        //I TRY TO MAKE LIKE ABOVE BUT MAYBE IT'S NOT LIKE THAT

        Console.SetCursorPosition(x1, y1);
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Your answer : ");

        string answer;
        int chance = 0;
        do
        {
            Console.SetCursorPosition(x2, y2);
            Console.WriteLine("                                                                          ");
            Console.SetCursorPosition(x2, y2);
            answer = Console.ReadLine();
            chance++;
            if (answer != correctanswer1 || answer != correctanswer2 || answer != correctanswer3)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.SetCursorPosition(x2, y2 + 1);
                Console.WriteLine("Sorry, your answer is incorrect...");
                Console.Beep();
                Console.ForegroundColor = ConsoleColor.White;
                Console.ReadKey(true);
                Console.SetCursorPosition(x2, y2 + 1);
                Console.WriteLine("                                   ");
            }
        } while (chance < count && answer != correctanswer1 || answer != correctanswer2 || answer != correctanswer3);

        if (answer == correctanswer1 || answer != correctanswer2 || answer != correctanswer3)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.SetCursorPosition(x2, y2 + 1);
            Console.WriteLine("Congratulation! Your answer is correct...");
            Console.ForegroundColor = ConsoleColor.White;
            Console.ReadKey(true);

            return true;//Return to Quiz Class
        }
        else
        {
            return false;

            // GameOver();
        }
    }
}
}
使用类似于foreach或for的迭代器