Console.ReadKey();和 Switch 语句 - 使用字母

发布于 2024-11-28 16:49:28 字数 2198 浏览 2 评论 0原文

我正在尝试用 C# 编写一个程序,该程序基本上通过使用 Console.ReadKey(); 来根据用户按下的键(例如 X = 退出、D = 断开等)运行;在 c# 中,

我遇到的问题是如何在 Switch 语句中使用 ReadKey 信息。有人可以帮忙吗?代码如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Switch_Test
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Welcome. Please enter your command: ");
            string chinput;
            int input;
            bool activated = false;
            input = Console.ReadKey();
            chinput = Convert.ToChar(input);
            switch (chinput)
            {
                case 'x':
                    {
                        Console.WriteLine("You pressed x...");
                        break;
                    }
                case 'y':
                    {
                        Console.WriteLine("You pressed y..");
                        break;
                    }
                case 'd':
                    {
                        if (activated != true)
                        {
                            Console.WriteLine("Please activate first!");
                            break;
                        }
                        else
                        {
                            Console.WriteLine("Active..");
                            break;
                        }
                    }
                case 'a':
                    {
                        if (activated != true)
                        {
                            activated = true;
                            Console.WriteLine("Activating..");
                            break;
                        }
                        else
                        {
                            activated = false;
                            Console.WriteLine("Deactivating.");
                            break;
                        }
                    }
                default:
                    Console.WriteLine("Unknown Command.");
                    break;
            }


        }
    }
}

我知道这可能真的是错误的,但我最初是从 Console.ReadLine(); 开始的,唯一的区别是我希望它在您按下单个键时激活一个功能,而不是必须按回车键或能够输入不同的键。提前致谢!

I'm trying to make a program in C# that basically functions based on the key a user presses (ex. X = Quit, D = Disconnect, etc;) by using Console.ReadKey(); in c#

The problem I'm running into is how to use the ReadKey info in a Switch statement.. Can someone help please? The code is below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Switch_Test
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Welcome. Please enter your command: ");
            string chinput;
            int input;
            bool activated = false;
            input = Console.ReadKey();
            chinput = Convert.ToChar(input);
            switch (chinput)
            {
                case 'x':
                    {
                        Console.WriteLine("You pressed x...");
                        break;
                    }
                case 'y':
                    {
                        Console.WriteLine("You pressed y..");
                        break;
                    }
                case 'd':
                    {
                        if (activated != true)
                        {
                            Console.WriteLine("Please activate first!");
                            break;
                        }
                        else
                        {
                            Console.WriteLine("Active..");
                            break;
                        }
                    }
                case 'a':
                    {
                        if (activated != true)
                        {
                            activated = true;
                            Console.WriteLine("Activating..");
                            break;
                        }
                        else
                        {
                            activated = false;
                            Console.WriteLine("Deactivating.");
                            break;
                        }
                    }
                default:
                    Console.WriteLine("Unknown Command.");
                    break;
            }


        }
    }
}

I know that's probably really wrong but I originally started with Console.ReadLine(); , the only difference is I want it to activate a function when you press a single key rather than having to hit enter or being able to type in different keys. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一杆小烟枪 2024-12-05 16:49:28

首先, Convert.ToChar() 不适用于 ConsoleKeyInfo 结构,因此这就是您遇到问题的原因,此转换将引发异常。

您不必将键转换为字符,而是可以打开 .Key 属性,该属性是一个包含每个键的枚举:

var input = Console.ReadKey();

switch (input.Key) //Switch on Key enum
{
    case ConsoleKey.X:
        break;
    case ConsoleKey.Y:
        break;
}

编辑:

  1. 您还可以使用 input.KeyChar 来获取您首先尝试的字符,然后您可以根据需要打开它,但是很难打开不同的键(例如箭头等)。
  2. 如果您关心字母是大写/小写,您可以使用 < code>.KeyChar 或使用.Key.Modifiers 来检查用户输入字母时是否按下了 Shift 键

First of all, Convert.ToChar() doesn't work on ConsoleKeyInfo structure, so that's why you have problems, this conversion will throw an Exception.

You don't have to convert your key to character, instead, you can switch on .Key property, which is an enumerable that contains every key:

var input = Console.ReadKey();

switch (input.Key) //Switch on Key enum
{
    case ConsoleKey.X:
        break;
    case ConsoleKey.Y:
        break;
}

Edit:

  1. You can also use input.KeyChar to get what you tried first - character, then you can switch on it if you want to, but it's harded to switch on different keys like arrows etc.
  2. If you care if letter is capital/small, you can use .KeyChar or use .Key with .Modifiers to check if shift key was pressed when user typed the letter
烟酒忠诚 2024-12-05 16:49:28

您可以简单地将输入视为:

char input=Console.ReadKey().KeyChar;

You can simply take input as:

char input=Console.ReadKey().KeyChar;
献世佛 2024-12-05 16:49:28

使用 Console.ReadKey() 返回 struct ConsoleKeyInfo 的类型。因此您需要从该类型的变量中接收返回值。
然后打开包含所有字符的键枚举器。

 ConsoleKeyInfo chinput = Console.ReadKey();

        switch (chinput.Key)
        {
            case ConsoleKey.X:

                { 
                    Console.WriteLine("You pressed x...");
                    break;
                }
            case ConsoleKey.Y:
                {
                    Console.WriteLine("You pressed y..");
                    break;
                }
            case ConsoleKey.D:
                {
                    if (activated != true)
                    {
                        Console.WriteLine("Please activate first!");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Active..");
                        break;
                    }
                }
            case ConsoleKey.A:
                {
                    if (activated != true)
                    {
                        activated = true;
                        Console.WriteLine("Activating..");
                        break;
                    }
                    else
                    {
                        activated = false;
                        Console.WriteLine("Deactivating.");
                        break;
                    }
                }
            default:
                Console.WriteLine("Unknown Command.");
                break;
        } 

using Console.ReadKey() returns a type of a struct ConsoleKeyInfo. so you need to recieve the return in a variable from this type.
Then switch on the Key enumrator, that has all characters.

 ConsoleKeyInfo chinput = Console.ReadKey();

        switch (chinput.Key)
        {
            case ConsoleKey.X:

                { 
                    Console.WriteLine("You pressed x...");
                    break;
                }
            case ConsoleKey.Y:
                {
                    Console.WriteLine("You pressed y..");
                    break;
                }
            case ConsoleKey.D:
                {
                    if (activated != true)
                    {
                        Console.WriteLine("Please activate first!");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Active..");
                        break;
                    }
                }
            case ConsoleKey.A:
                {
                    if (activated != true)
                    {
                        activated = true;
                        Console.WriteLine("Activating..");
                        break;
                    }
                    else
                    {
                        activated = false;
                        Console.WriteLine("Deactivating.");
                        break;
                    }
                }
            default:
                Console.WriteLine("Unknown Command.");
                break;
        } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文