我应该在我的输入框中放置什么代码?

发布于 2024-12-19 09:18:21 字数 1133 浏览 2 评论 0原文

我制作了一个开关盒,使用向上箭头键、向下箭头键和回车键。但是我想不出如何将代码放入我可以选择选项的输入案例中。

public static void entries()
    {
        keyPressed = Console.ReadKey(true);
        switch (keyPressed.Key)
        {

            case ConsoleKey.DownArrow:
                if (keyPressed.Key.ToString() == "DownArrow")// selects the curitem when the down arrow key is pressed
                {
                    curItem++;
                    if (curItem > menuItems.Length - 1) curItem = 0;
                }
                break;
            case ConsoleKey.UpArrow:
                if (keyPressed.Key.ToString() == "UpArrow")// selects the curitem when the up arrow key is pressed
                {
                    curItem--;
                    if (curItem < 0) curItem = Convert.ToInt16(menuItems.Length - 1);

                }
                break;
            case ConsoleKey.Enter:
                if (keyPressed.Key.ToString() == "Enter")// when enter is pressed it will go to one of the choices 
                {

                }
                break;
            default:
                break;
        }
    }

I've made a switch-case where I use the up arrow key, down arrow key and enter key. However I can't think of how to put code into my enter case in which I can choose an option.

public static void entries()
    {
        keyPressed = Console.ReadKey(true);
        switch (keyPressed.Key)
        {

            case ConsoleKey.DownArrow:
                if (keyPressed.Key.ToString() == "DownArrow")// selects the curitem when the down arrow key is pressed
                {
                    curItem++;
                    if (curItem > menuItems.Length - 1) curItem = 0;
                }
                break;
            case ConsoleKey.UpArrow:
                if (keyPressed.Key.ToString() == "UpArrow")// selects the curitem when the up arrow key is pressed
                {
                    curItem--;
                    if (curItem < 0) curItem = Convert.ToInt16(menuItems.Length - 1);

                }
                break;
            case ConsoleKey.Enter:
                if (keyPressed.Key.ToString() == "Enter")// when enter is pressed it will go to one of the choices 
                {

                }
                break;
            default:
                break;
        }
    }

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

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

发布评论

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

评论(2

残花月 2024-12-26 09:18:21

您关于嵌套 if 语句的想法是正确的,或者有另一个开关。我可能会将其重新分解为另一个函数:

case ConsoleKey.Enter:
   chooseOption(curItem);
   break;

...

void chooseOption(int item)
{
   switch(item)
   {
      case 1:
        //Do item 1
        break;
      case 2:
        //Do item 2
        break;
      case 3:
        //Do item 3
        break;
   }
}

Your idea about nesting if statements is correct, or having another switch. I would probably re-factor this into another function though:

case ConsoleKey.Enter:
   chooseOption(curItem);
   break;

...

void chooseOption(int item)
{
   switch(item)
   {
      case 1:
        //Do item 1
        break;
      case 2:
        //Do item 2
        break;
      case 3:
        //Do item 3
        break;
   }
}
萧瑟寒风 2024-12-26 09:18:21

根据您的评论。以下是如何显示 menuItems 数组中的当前项目。我假设 menuItems 包含一些字符串或整数或可以轻松写入控制台的内容。

case ConsoleKey.Enter:
// when enter is pressed it will go to one of the choices 
   Console.Clear();
   Console.WriteLine(menuItems[curItem]);
   break;

Based on your comment. here is how you can display the current item inside the menuItems array. I'm assuming menuItems holds some strings or ints or something that can be easily written to the console.

case ConsoleKey.Enter:
// when enter is pressed it will go to one of the choices 
   Console.Clear();
   Console.WriteLine(menuItems[curItem]);
   break;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文