你可以使用数组作为 Switch(case) 吗?

发布于 2024-12-18 22:28:25 字数 2109 浏览 0 评论 0原文

我已经开始了大学课程,但我无法了解如何使用我的阵列作为开关盒。基本上我只需要开关盒的帮助,然后我就可以继续我的工作了。 到目前为止,情况如下:

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

namespace Assignment2
{
 class Program
  {
    public const int noOfentries = 6;
    public const int address = 5;
    public static string[,] addressBook = new string[noOfentries, address];//
    string array for the address book
    public static int deletion;
    public static int choice;
    public static ConsoleKeyInfo keyPressed;
    public static short curItem = 0, c;
    public static string[,] menuItems = new string[,]
    { 
        {"Add Entry"},
        {"Delete Entry"},
        {"Print Book to Screen"},
        {"Edit Contact"}, 
        {"Exit"} 
    };


    #region addEntry
    #endregion
    #region deleteEntry
    #endregion
    #region seeBook
    #endregion
    public static void fourthChoice()
    {
        Console.WriteLine("Would you like to edit the name or address?");

    }

    public static void menu()
    { 
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine(menuItems[i,0].PadRight(10));
            Console.Clear();
            for (c = 0; c < menuItems.Length; c++)
            {
                if (curItem == c)
                {
                    Console.Write(">");
                    Console.WriteLine(menuItems[c,0]);
                    Console.ForegroundColor = ConsoleColor.Green;
                }

                else
                {
                    Console.WriteLine(menuItems[c,0]);
                }
            }
            Console.WriteLine("Please select an option with the Arrow Keys");
         }
    }
    public static void entries()
    {
        switch (menuItems[0,0])
        {
            case "Add Entry":
                break;
            case "Delete Entry":
                break;
            case "Print Book to Screen":
                break;
            case "Edit Contact":
                break;
            case "Exit":
                break;

        }
    }

I've started a Uni course and I can't get me head around how to use my array for a switch case. Basically I just need the help with the switch-case, then I can get on with my work.
Heres what it looks like so far:

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

namespace Assignment2
{
 class Program
  {
    public const int noOfentries = 6;
    public const int address = 5;
    public static string[,] addressBook = new string[noOfentries, address];//
    string array for the address book
    public static int deletion;
    public static int choice;
    public static ConsoleKeyInfo keyPressed;
    public static short curItem = 0, c;
    public static string[,] menuItems = new string[,]
    { 
        {"Add Entry"},
        {"Delete Entry"},
        {"Print Book to Screen"},
        {"Edit Contact"}, 
        {"Exit"} 
    };


    #region addEntry
    #endregion
    #region deleteEntry
    #endregion
    #region seeBook
    #endregion
    public static void fourthChoice()
    {
        Console.WriteLine("Would you like to edit the name or address?");

    }

    public static void menu()
    { 
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine(menuItems[i,0].PadRight(10));
            Console.Clear();
            for (c = 0; c < menuItems.Length; c++)
            {
                if (curItem == c)
                {
                    Console.Write(">");
                    Console.WriteLine(menuItems[c,0]);
                    Console.ForegroundColor = ConsoleColor.Green;
                }

                else
                {
                    Console.WriteLine(menuItems[c,0]);
                }
            }
            Console.WriteLine("Please select an option with the Arrow Keys");
         }
    }
    public static void entries()
    {
        switch (menuItems[0,0])
        {
            case "Add Entry":
                break;
            case "Delete Entry":
                break;
            case "Print Book to Screen":
                break;
            case "Edit Contact":
                break;
            case "Exit":
                break;

        }
    }

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

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

发布评论

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

评论(2

旧瑾黎汐 2024-12-25 22:28:25

switch/case 有两个方面:

  • 您要切换的值
  • 您想要分支到的情况

您从数组获取值的事实是无关紧要的。它相当于:

string value = menuItems[0, 0];
switch (value)
{
}

您的情况也是常量字符串值,所以这也很好。 (那里存在重复,导致您可能想要解决脆弱的代码,但这是一个单独的问题。)

...这很好。目前还不清楚您遇到了什么问题,尽管不清楚为什么您有一个矩形数组,因为每行只有一个“列” 。为什么不只是:(

public static string[] menuItems = new string[]
{ 
    "Add Entry",
    "Delete Entry",
    "Print Book to Screen",
    "Edit Contact", 
    "Exit"
};

抛开命名、可访问性、可变性等)

There are two aspects to a switch/case:

  • The value you're switching on
  • The cases you want to branch to

The fact that you're getting the value from an array is irrelevant. It's equivalent to:

string value = menuItems[0, 0];
switch (value)
{
}

Your cases are also constant string values, so that's fine too. (There's duplication there leading to fragile code which you may want to address, but that's a separate matter.)

... and that's fine. It's not really clear what problem you're having at the moment, although it's also unclear why you've got a rectangular array at all, given that you've only got a single "column" per row. Why not just:

public static string[] menuItems = new string[]
{ 
    "Add Entry",
    "Delete Entry",
    "Print Book to Screen",
    "Edit Contact", 
    "Exit"
};

(Leaving aside naming, accessibility, mutability etc.)

画骨成沙 2024-12-25 22:28:25

您应该为此使用 Enum:

private Enum myEnum { Add, Delete, Edit }

void main(myEnum state)
{
    switch (state)
    {
       Add: //do things
            break;
       Edit: //do things
            break;
       Delete: //do things
               break;
    }
}

You should use an Enum for that:

private Enum myEnum { Add, Delete, Edit }

void main(myEnum state)
{
    switch (state)
    {
       Add: //do things
            break;
       Edit: //do things
            break;
       Delete: //do things
               break;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文