📜  C#中的Console.ReadKey()方法

📅  最后修改于: 2021-05-29 15:28:31             🧑  作者: Mango

Console.ReadKey()方法使程序等待按键,并且直到按键被按下才阻止屏幕显示。简而言之,它获取下一个字符或用户按下的任何键。按下的键将显示在控制台窗口中(如果将进行任何输入过程)。此方法的重载列表中有两种方法,如下所示:

  • ReadKey()方法
  • ReadKey(Boolean)方法

ReadKey()方法

此方法用于获取用户按下的下一个字符或函数键。按下的键显示在控制台窗口中。

下面的程序说明了上述方法的用法:

范例1:

// C# program to illustrate the
// Console.ReadKey Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        int c = 0;
        Console.WriteLine("The series is:");
  
        for (int i = 1; i < 10; i++) 
        {
            c = c + i;
            Console.Write(c + " ");
        }
  
        Console.WriteLine("\npress any key to exit the process...");
      
        // basic use of "Console.ReadKey()" method
        Console.ReadKey();
          
    }
}

输出:

范例2:

// C# program to illustrate the
// Console.ReadKey Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        int c = 0;
        Console.WriteLine("The series is:");
        for (int i = 1; i < 10; i++) 
        {
            c = c + i;
            Console.Write(c + " ");
        }
  
        Console.Write("\nPress 'Enter' to exit the process...");
  
        // another use of "Console.ReadKey()" method
        // here it asks to press the enter key to exit
        while (Console.ReadKey().Key != ConsoleKey.Enter) {
        }
          
    }
}

输出:

范例3:

// C# program to illustrate the
// Console.ReadKey Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
          
        // "DateTime" is a inbuilt class 
        // for date and time
        DateTime d = DateTime.Now;
          
        // print the system date and time
        Console.WriteLine("System date: {0:d}\n"+
                        "System time: {0:t}", d);
          
        Console.Write("Press 'E' to exit the process...");
  
        // here it ask to press "E" to exit
        while (Console.ReadKey().Key != ConsoleKey.E) {
        }
          
    }
}

输出:

ReadKey(Boolean)方法

此方法与先前的方法更相似,也就是说,它还获得用户按下的下一个字符或任何键。唯一的区别是所选择的键可以有选择地显示在控制台窗口中。

下面的程序说明了上述方法的用法:

范例1:

// C# program to illustrate the 
// ReadKey(Boolean) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        int c = 0;
        Console.WriteLine("The series is-");
        for (int i = 1; i < 10; i++) 
        {
            c = c + i;
            Console.Write(c + " ");
        }
  
        Console.WriteLine("\npress any key to exit the process...");
  
        // here we use "false" in the argument list
        // when we press any key, the key will 
        // displays in the console output window
        Console.ReadKey(false);
         
    }
}

输出:

范例2:

// C# program to illustrate the 
// ReadKey(Boolean) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        int c = 0;
        Console.WriteLine("The series is-");
        for (int i = 1; i < 10; i++) 
        {
            c = c + i;
            Console.Write(c + " ");
        }
        Console.Write("\nPress 'E' to exit the process...");
  
        // here it asks to press "E" to exit
        // and the key "E" is not shown in
        // the console output window
        while (Console.ReadKey(true).Key != ConsoleKey.E) {
        }
         
    }
}

输出:

范例3:

// C# program to illustrate the 
// ReadKey(Boolean) Method
using System;
  
class GFG {
  
    public static void Main()
    {
        // "DateTime" is a inbuilt class
        // for date and time
        DateTime d = DateTime.Now;
          
        // print the system date and time
        Console.WriteLine("System date: {0:d}\n"+
                        "System time: {0:t}", d);
  
        Console.Write("Press 'E' to exit the process...");
  
        // here it asks to press "E" to exit
        // The key "E" is shown in the console 
        // output window because of "false"
        while (Console.ReadKey(false).Key != ConsoleKey.E) {
        }
          
    }
}

输出: