【问题标题】:Pass 2D Array Info to a method in c#将二维数组信息传递给 c# 中的方法
【发布时间】:2023-03-16 14:36:02
【问题描述】:

我有一个评估,我们必须在应用程序中实现方法以将员工信息存储在 2D 数组中并在屏幕上显示。到目前为止,代码可以像这样工作,但我无法找到将二维数组信息传递给开关的 case 2 的方法。当我尝试从 UserInput 方法返回数组信息或为案例 2 创建方法并尝试传递数组时,它总是会导致 IndexOutOfRangeException。这是我的代码,在此先感谢您的帮助:

using System;

namespace EmployeeFileWithMethods
{
  class Program
  {
    static void Main(string[] args)
    {
      int numberEmployees = 0;
      string[,] table = new string[numberEmployees, 4];
      string userInput = "1";
      while ( userInput != "0" )
      {

        userInput = Intro();

        switch ( userInput )
        {
          case "1":
            UserInput();
            break;
          case "2":
            Console.Clear();
            for ( int user = 0; user < table.GetLength(0); user++ )
            {
              Console.WriteLine("     User " + ( user + 1 ));
              for ( int row = 0; row < table.GetLength(1); row++ )
              {
                Console.Write(table[user, row] + "\n");
              }
              Console.WriteLine("");
            }
            break;
          case "0":
            break;
          default:
          {
            DefaultCase();
            break;
          }

        }

      }
      Console.WriteLine("Thanks for using the app!");
      Console.ReadLine();
    }
    public static string Intro()
    {
      Console.WriteLine("[-------------------------------------------------------------------------------]");
      Console.WriteLine("                        Welcome to the Edinburgh College App \n                             What would you like to do?\n                                   1:Add User\n                                   2:Show User Info\n                                   0:Exit");
      Console.WriteLine("[-------------------------------------------------------------------------------]");
      string userInput = Console.ReadLine();
      return userInput;
    }
    public static void DefaultCase()
    {
      Console.Clear();
      Console.WriteLine("[-------------------------------------------------------------------------------]");
      Console.WriteLine("              The option that you entered is invalid. Please try again.              ");
      Console.WriteLine("[-------------------------------------------------------------------------------]");
    }
    public static void UserInput()
    {
      Console.WriteLine("How many employees does your company have?");
      int numberEmployees = Convert.ToInt32(Console.ReadLine());
      string[,] table = new string[numberEmployees, 4];
      for ( int row = 0; row < numberEmployees; row++ )
      {
        Console.WriteLine("Write the Forename of user " + ( row + 1 ));
        string forename = Console.ReadLine();
        table[row, 0] = forename;
        Console.WriteLine("Write the Surname of user " + ( row + 1 ));
        string surname = Console.ReadLine();
        table[row, 1] = surname;

        while ( true )
        {
          Console.WriteLine("Write the Phone of user " + ( row + 1 ));
          string phone = Console.ReadLine();
          if ( phone.Length == 11 )
          {
            table[row, 2] = phone;
            break;
          }
          else
          {
            Console.WriteLine("Invalid Phone Number. Please Try Again");
            continue;
          }
        }
        while ( true )
        {
          Console.WriteLine("Write the Email of user " + ( row + 1 ));
          string email = Console.ReadLine();
          int charPos = email.IndexOf('@');
          if ( charPos > 0 )
          {
            table[row, 3] = email;
            break;
          }
          else
          {
            Console.WriteLine("Invalid Email. Please Try Again");
            continue;
          }
        }

      }
    }
  }

}

【问题讨论】:

    标签: c# arrays multidimensional-array methods


    【解决方案1】:

    我无法重现该异常,但UserInput 不返回任何内容,并且在此方法中初始化的表在返回时丢失。所以Main 中的表格的第一个暗淡的大小为 0。您应该通过 ref 将表格作为参数传递并删除方法中的表格声明:

    UserInput(table);
    
    public static void UserInput(ref string[,] table)
    

    但是你需要调整这个数组的大小来添加新的输入。

    更好、更简单、更健壮、更简洁的方法是使用类实体列表。这是经过调整和改进以使用员工实体列表的代码。我对代码进行了最低限度的修改,但可以对其进行更多改进和重构,尤其是 while 循环,您也可以使用 int.TryParse 来添加要添加的员工数量。

    using System.Collections.Generic;
    
    public class Employee
    {
      public string Forename { get; set; }
      public string Surname { get; set; }
      public string Phone { get; set; }
      public string Email { get; set; }
    }
    
    static private void Main()
    {
      var employees = new List<Employee>();
      string userInput = "1";
      while ( userInput != "0" )
      {
        userInput = Intro();
        switch ( userInput )
        {
          case "1":
            UserInput(employees);
            break;
          case "2":
            Console.Clear();
            for ( int index = 0; index < employees.Count; index++ )
            {
              Console.WriteLine("     User " + ( index + 1 ));
              Console.WriteLine("         Forename: " + employees[index].Forename);
              Console.WriteLine("         Surname: " + employees[index].Surname);
              Console.WriteLine("         Phone: " + employees[index].Phone);
              Console.WriteLine("         eMail: " + employees[index].Email);
              Console.WriteLine("");
            }
            break;
          case "0":
            break;
          default:
          {
            DefaultCase();
            break;
          }
    
        }
    
      }
      Console.WriteLine("Thanks for using the app!");
      Console.ReadLine();
    }
    
    public static void UserInput(List<Employee> employees)
    {
      Console.WriteLine("How many employees does your company have?");
      int countEmployees = employees.Count;
      int countEmployeesNew = Convert.ToInt32(Console.ReadLine());
      for ( int indexEmployeeNew = 0; indexEmployeeNew < countEmployeesNew; indexEmployeeNew++ )
      {
        int posEmployeeNew = countEmployees + indexEmployeeNew + 1;
        Console.WriteLine("Write the Forename of user " + posEmployeeNew);
        string forename = Console.ReadLine();
        Console.WriteLine("Write the Surname of user " + posEmployeeNew);
        string surname = Console.ReadLine();
        string phone = "";
        while ( true )
        {
          Console.WriteLine("Write the Phone of user " + posEmployeeNew);
          phone = Console.ReadLine();
          if ( phone.Length == 11 ) break;
          Console.WriteLine("Invalid Phone Number. Please Try Again");
        }
        string email = "";
        while ( true )
        {
          Console.WriteLine("Write the Email of user " + posEmployeeNew);
          email = Console.ReadLine();
          int charPos = email.IndexOf('@');
          if ( charPos > 0 ) break;
          Console.WriteLine("Invalid Email. Please Try Again");
        }
        employees.Add(new Employee
        {
          Forename = forename,
          Surname = surname,
          Phone = phone,
          Email = email
        });
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-18
      • 2021-01-07
      • 2021-12-15
      • 2021-05-10
      相关资源
      最近更新 更多