C# 将2D数组信息传递给c中的方法#
我有一个评估,我们必须在应用程序中实现方法,以2D数组存储员工信息并在屏幕上显示。到目前为止,代码可以像这样工作,但我无法找到一种方法将2D数组信息传递给交换机的案例2。当我尝试从UserInput方法返回数组信息时,或者为案例2创建方法并尝试传递数组时,它总是导致IndexOutOfRangeException。这是我的代码,提前感谢您的帮助:C# 将2D数组信息传递给c中的方法#,c#,arrays,multidimensional-array,methods,C#,Arrays,Multidimensional Array,Methods,我有一个评估,我们必须在应用程序中实现方法,以2D数组存储员工信息并在屏幕上显示。到目前为止,代码可以像这样工作,但我无法找到一种方法将2D数组信息传递给交换机的案例2。当我尝试从UserInput方法返回数组信息时,或者为案例2创建方法并尝试传递数组时,它总是导致IndexOutOfRangeException。这是我的代码,提前感谢您的帮助: using System; namespace EmployeeFileWithMethods { class Program {
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;
}
}
}
}
}
}
使用系统;
命名空间EmployeeFileWithMethods
{
班级计划
{
静态void Main(字符串[]参数)
{
int numberEmployees=0;
字符串[,]表=新字符串[numberEmployees,4];
字符串userInput=“1”;
while(userInput!=“0”)
{
userInput=Intro();
开关(用户输入)
{
案例“1”:
用户输入();
打破
案例“2”:
Console.Clear();
for(int user=0;user0)
{
表[第3行]=电子邮件;
打破
}
其他的
{
Console.WriteLine(“无效电子邮件,请重试”);
继续;
}
}
}
}
}
}
我无法重现异常,但是UserInput
不返回任何内容,并且在返回时,在此方法中初始化的表将丢失。因此,Main
中的表的第一个dim的大小为0。应通过ref将表作为参数传递,并删除方法中的表声明:
UserInput(table);
public static void UserInput(ref string[,] table)
但您需要调整此数组的大小以添加新的输入
一种更好、更简单、更健壮、更干净的方法是使用类实体的列表。下面是经过修改和改进的代码,用于使用员工实体的列表。我对代码做了最低限度的修改,但可以进一步改进和重构,尤其是while
循环,您还可以使用int.TryParse
来增加员工数量
using System.Collections.Generic;
static private void Main()
{
var employees=新列表();
字符串userInput=“1”;
while(userInput!=“0”)
{
userInput=Intro();
开关(用户输入)
{
案例“1”:
用户输入(员工);
打破
案例“2”:
Console.Clear();
对于(int index=0;index
publicstaticvoiduserinput(列出员工)
{
Console.WriteLine(“贵公司有多少员工?”);
int countEmployees=employees.Count;
int countEmployeesNew=Convert.ToInt3
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
});
}
}