如何读取用户按下的键并将其显示在控制台上? [英] How to read a key pressed by the user and display it on the console?

查看:128
本文介绍了如何读取用户按下的键并将其显示在控制台上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要求用户输入任何键,当该键被按下时,它显示You Pressed'Key'。

I am trying to ask user "enter any key" and when that key is pressed it shows that "You Pressed 'Key'". Can you help what's wrong in this code?

这是我写的:

using System;
class Program
{
    public static void Main(string[] args)
    {      
        Console.Write("Enter any Key: ");
        char name = Console.Read();
        Console.WriteLine("You pressed {0}", name);
    }
}


推荐答案

Console.WriteLine("Enter any Key: ");
ConsoleKeyInfo name = Console.ReadKey();
Console.WriteLine("You pressed {0}", name.KeyChar);

这篇关于如何读取用户按下的键并将其显示在控制台上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆