什么是一次写一个字符的简单方法

时间:2018-01-09 08:28:42

标签: methods char

我必须继续为每个Writeline输入相同的4行程序,我可以制作一种方法还是一种更简单的方法?

static void Main(string[] args)
        {
            string text = "You wake up, you raise your head off the floor";
            foreach (char c in text)
            {
                Console.Write(c);
                Thread.Sleep(50);
            }
            Thread.Sleep(200);
            Console.WriteLine("\nPress Enter to continue");
            Console.ReadLine();
            text = "You gather your senses, and you...";
            foreach (char c in text)
            {
                Console.Write(c);
                Thread.Sleep(50);
            }

1 个答案:

答案 0 :(得分:1)

您必须定义自己的方法:

static void WriteCharByChar(string text) 
{
   foreach (char c in text)
   {
      Console.Write(c);
      Thread.Sleep(50);
   }
}

然后只需称呼它:

static void Main(string[] args)
{
   string text = "You wake up, you raise your head off the floor";
   WriteCharByChar(text);
   Thread.Sleep(200);
   Console.WriteLine("\nPress Enter to continue");
   Console.ReadLine();
   text = "You gather your senses, and you...";
   WriteCharByChar(text);
}