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

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

问题描述

我想询问用户输入任意键,当按下此键,显示您按'关键'。你能帮什么是错的代码?



这是我自己写的:

 使用系统; 
类节目
{
酒店的公共静态无效的主要(字串[] args)
{

Console.Write(输入密钥:);
字符名称= Console.Read() ;
Console.WriteLine(你按下{0},名称);
}
}


解决方案

尝试

  Console.WriteLine(输入任意键:); 
ConsoleKeyInfo名= Console.ReadKey();
Console.WriteLine(你按下{0},name.KeyChar);


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?

This is what I have written:

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

解决方案

Try

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

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

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