使用Switch语句调用的C#方法-遇到麻烦

时间:2018-08-07 08:47:01

标签: c#

我是编程新手,我试图理解为什么switch语句内的方法调用不起作用。基本上,我有一个主菜单和一个子菜单。我按下Animal子菜单后,switch语句旨在调用CRUD的方法(插入新的,按ID可视化,更新等),但这些选项均无效。方法调用/结构正确吗?

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

namespace menu_clinica_veterinaria
{
    class Program
    {
        public static int id = 1;
        enum animalHeader { id, name, client_name, type_animal };
        enum clientHeader { id, name, client_surname, adrress };

        static void Main(string[] args)
        {
            string[,] animal = new string[20, 4];
            string[,] client = new string[20, 6];

            do { MenuOptions(animal); } while (true);
        }

        static void MenuOptions(string[,] animal)
        {
            int userChoice;

            do
            {
                Console.Clear();
                Console.WriteLine("\nChoose one of the following options:\n");

                Console.WriteLine("[ 1 ] Animals");
                Console.WriteLine("[ 2 ] Clients");
                Console.WriteLine("[ 0 ] Quit application\n");
            } while (!int.TryParse(Console.ReadLine(), out userChoice) || userChoice < 0 || userChoice > 2);

            Console.Clear();

            switch (userChoice)
            {
                case 1:
                    menuAnimal(animal);
                    break;

                case 2:
                    //menuClient(client);
                    break;

                case 0:
                    Environment.Exit(0);
                    break;

                default:
                    Console.WriteLine("Try again!!");
                    break;
            }
        }

        static void menuAnimal(string[,] animal)
        {
            int optAnimal;

            while (true)
            {
                do
                {
                    Console.Clear();
                    Console.WriteLine("\nInsert one of the following options:\n");

                    Console.WriteLine("[ 1 ] Insert animal");
                    Console.WriteLine("[ 2 ] See animal");
                    Console.WriteLine("[ 3 ] Alter animal");
                    Console.WriteLine("[ 4 ] Erase animal");
                    Console.WriteLine("[ 5 ] List animals");
                    Console.WriteLine("[ 0 ] Return to main menu\n");
                } while (!int.TryParse(Console.ReadLine(), out optAnimal) || optAnimal < 0 || optAnimal > 5);

                Console.Clear();
                bool goBack = false;

                switch (optAnimal)
                {
                    case 1:
                        insertData(animal);
                        break;

                    case 2:
                        visualizeByid(animal);
                        break;

                    case 3:
                        updateById(animal);
                        break;

                    case 4:
                        deleteByid(animal);
                        break;

                    case 5:
                        listData(animal);
                        break;

                    case 0:
                        goBack = true;
                        break;
                }

                if (goBack) return;
            }
        }

        static void mainMenu()
        {
            Console.Clear();
            Console.ReadKey();
        }

        static void menuReturn(string[,] animal)
        {
            Console.Clear();

            do { menuAnimal(animal); } while (true);
        }

        static int generateId()
        {
            return id++;
        }

        static int getInsertIndex(string[,] matrix)
        {
            for (int j = 0; j < matrix.GetLength(0) - 1; j++)
            {
                if (string.IsNullOrEmpty(matrix[j, 0])) return j;
            }

            return -1;
        }

        static void insertData(string[,] matrix)
        {
            int id = generateId();
            int n = getInsertIndex(matrix);

            matrix[n, 0] = Convert.ToString(id);

            for (int j = 1; j < matrix.GetLength(1); j++)
            {
                do
                {
                    Console.Write($"Insert {Enum.GetName(typeof(animalHeader), j)}: ");
                    matrix[n, j] = Console.ReadLine();
                } while (String.IsNullOrEmpty(matrix[n, j]));
            }
        }

        static int searchId(string[,] matrix)
        {
            int choosenId, index = -1;

            do
            {
                Console.Write("Insert ID to continue: ");
            } while (!int.TryParse(Console.ReadLine(), out choosenId));

            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                if (Convert.ToString(choosenId) == matrix[i, 0])
                {
                    index = i;
                }
            }

            return index;
        }

        static void visualizeByid(string[,] matrix)
        {
            int pos = searchId(matrix);

            if (pos != -1)
            {
                for (int i = pos; i < pos + 1; i++)
                {
                    for (int j = 0; j < matrix.GetLength(1); j++)
                    {
                        Console.Write($"{matrix[i, j]}\t");
                    }
                    Console.WriteLine();
                }
            }
            else { Console.WriteLine("Wrong Id"); }
        }

        static void updateById(string[,] matrix)
        {
            int pos = searchId(matrix);

            if (pos != -1)
            {
                for (int i = pos; i < pos + 1; i++)
                {
                    for (int j = 1; j < matrix.GetLength(1); j++)
                    {
                        Console.Write($"Insert {Enum.GetName(typeof(animalHeader), j)}:  ");
                        matrix[i, j] = Console.ReadLine();
                    }

                    Console.WriteLine();
                }
            }
            else { Console.WriteLine("Id does not exist"); }
        }

        static void deleteByid(string[,] matrix)
        {
            int pos = searchId(matrix);

            if (pos != -1)
            {
                for (int i = pos; i < pos + 1; i++)
                {
                    for (int j = 0; j < matrix.GetLength(1); j++)
                    {
                        matrix[i, j] = null;
                    }
                }
            }
            else { Console.WriteLine("Id does not exist"); }
        }

        static void listData(string[,] matrix)
        {
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write($"\t{matrix[i, j]}\t");
                }

                Console.WriteLine("\n\t");
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

它是一个漂亮的小程序

但是,您看不到动物,因为

searchId

for (var i = 0; i < matrix.GetLength(0); i++)
{
   if (Convert.ToString(choosenId) == matrix[i, 0])
   {
      index = i;
      // Though its not the problem you could break here
      // you could also just return from here
      break;
   }
}

也。您需要Console.ReadKey();刷新菜单并覆盖动物视图例程的某个地方

菜单动物

visualizeByid(animal);

// if you don't do something like this as the menu refreshes the animal away
Console.ReadKey();

id唯一建议的另一件事是使用列表和类型良好的类,而不是多维的字符串数组,它们更易于使用,并且您可以使用Linq,yehhaa


更新

关于评论

给出这个

private static string GetHeader<T>(int i) => Enum.GetName(typeof(T), i);

您可能会这样

static void updateById<T>(string[,] matrix)
{
   int pos = searchId(matrix);
   if (pos != -1)
   {
      for (int i = pos; i < pos + 1; i++)
      {
         for (int j = 1; j < matrix.GetLength(1); j++)
         {
            Console.Write($"Insert {GetHeader<T>(j)}:  ");
            ...

用法

updateById<animalHeader>(animal);

基本上,这是一些重用updateById方法并使用您的HeaderType的泛型

答案 1 :(得分:-2)

您的程序运行正常。您正在使用Console.Clear()。它将清除先前显示在屏幕上的屏幕。删除程序中的Console.Clear()或在Switch块之后立即添加Console.ReadKey()。我认为会很好。