【问题标题】:How to make method from a class to array?如何将方法从类转换为数组?
【发布时间】:2014-06-29 13:30:22
【问题描述】:

目前我正在 C# 控制台应用程序中制作一个问答游戏。我还将测验问题放在课堂上的每个方法中。但是,我有随机测验,因此当用户再次玩游戏时,他们不会遇到与以前相同的测验顺序。我的问题是如何在 Array 中的一个类中创建一个方法到另一个类?我希望当我在 Quiz 类 Quiz 中调用一个方法时,它会是随机的。

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
}

我想从这个类中调用 Quiz 类中的方法

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

感谢您的帮助。 :)

【问题讨论】:

  • 使用像foreachfor这样的迭代器

标签: c# arrays methods


【解决方案1】:

以下内容只是为了让您了解它是如何工作的,我使用了一个 Action 代表列表并随机调用它们。

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

【讨论】:

    【解决方案2】:

    使用像foreachfor 这样的迭代器来构建你的数组;但是,在您的情况下,您看起来不像是在存储值;相反,您正在设置文本框的值,因此您可以调用返回 voids 的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2012-12-06
      • 2019-08-18
      相关资源
      最近更新 更多