简体   繁体   English

使用数组在C#中制作通讯录

[英]Making an address book in C# using Arrays

I'm new here but basically I need some help. 我是新来的,但基本上我需要一些帮助。 I've got a university assignment and I'm having some trouble with it. 我有大学作业,但遇到一些麻烦。

I'm trying to make an address book in C# as a console application and I've built my array and my case statement etc but I've got to a point where I don't know how to edit/add data to it. 我正在尝试使用C#作为控制台应用程序来创建通讯簿,并且已经构建了数组和我的case语句等,但是到了某种程度,我不知道如何编辑/添加数据。

The array is populated with "XXXXX" and I'm supposed to be able to change those to add first name, surname, house number&road name, state and zip code with row 1 being first name, row 2 being surname etc. Will the code I have written in case 1 do this or have I done that wrong? 数组中填充了“ XXXXX”,我应该能够更改这些名称以添加名字,姓氏,门牌号码和道路名称,州和邮政编码,第1行是名字,第2行是姓氏等。代码我写过第一种情况,还是做错了?

Also, case 3 is supposed to print the book to the screen but it is coming up with an error message saying it does not recognize s1, s2, s3, s4, s5 when I have declared them in the array. 同样,情况3应该将书印刷到屏幕上,但是出现错误消息,说我在数组中声明了s1,s2,s3,s4,s5后无法识别。 Is this because the array is separate to the rest of the program? 这是因为数组与程序的其余部分是分开的吗?

here is what I have got so far, it may very well be completely wrong but any help at all will be greatly appreciated. 这是到目前为止我得到的,很可能是完全错误的,但是对您的任何帮助将不胜感激。

Thanks alot. 非常感谢。

using System;

public static void Main()
{
    string s1, s2, s3, s4, s5;
    string select;
    string input;
    char charselect;
    char answer;

    string[,] a = new string[10, 5]
        {
            {"XXXXX", "XXXXX", "XXXXX", "XXXXX", "XXXXX"},
            {"XXXXX", "XXXXX", "XXXXX", "XXXXX", "XXXXX"},
            {"XXXXX", "XXXXX", "XXXXX", "XXXXX", "XXXXX"},
            {"XXXXX", "XXXXX", "XXXXX", "XXXXX", "XXXXX"},
            {"XXXXX", "XXXXX", "XXXXX", "XXXXX", "XXXXX"},
            {"XXXXX", "XXXXX", "XXXXX", "XXXXX", "XXXXX"},
            {"XXXXX", "XXXXX", "XXXXX", "XXXXX", "XXXXX"},
            {"XXXXX", "XXXXX", "XXXXX", "XXXXX", "XXXXX"},
            {"XXXXX", "XXXXX", "XXXXX", "XXXXX", "XXXXX"},
            {"XXXXX", "XXXXX", "XXXXX", "XXXXX", "XXXXX"}
         };

    for (int i = 0; i < a.Length / 5; i++)
    {
        s1 = a[i, 0];
        s2 = a[i, 1];
        s3 = a[i, 2];
        s4 = a[i, 3];
        s5 = a[i, 4];

    }
    Console.WriteLine("Do You Want to Open The Address Book?");
    Console.WriteLine("Type Y or N");
    input = Console.ReadLine();
    answer = Convert.ToChar(input);

    while (answer == 'Y')
    {
        Console.WriteLine("1. Add Entry");
        Console.WriteLine("2. Delete Entry");
        Console.WriteLine("3. Print Book to Screen");
        select = Console.ReadLine();
        {
            charselect = Convert.ToChar(select);
            char selection = (charselect);
            switch (selection)
            {
                case '1':
                    string firstname;
                    {
                        Console.WriteLine("Please enter the first name of your entry");
                        firstname = Console.ReadLine();


                        for (int i = 0; i < a.Length / 10; i++)
                        {
                            if (a[i, 0] == "XXXXX")
                            {
                                a[i, 0] = "firstname";
                            }
                        }
                        Console.ReadLine();
                        break;
                    }
                case '2':
                    {
                        Console.WriteLine("");
                        Console.ReadLine();
                        break;
                    }
                case '3':
                    {
                        Console.WriteLine("{0}, {1}, {2}, {3}, {4}", s1, s2, s3, s4, s5);
                        Console.ReadLine();
                        break;
                    }
                default:
                    {
                        Console.WriteLine("Incorrect selection, please try again");
                        Console.ReadLine();
                        break;
                    }
            }
        }
    }
}

Usually you would create a class containing the address fields: 通常,您将创建一个包含地址字段的类:

public class Address
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string Zip { get; set; }
}

Then work on an array (or list) of this Address type. 然后在此地址类型的数组(或列表)上工作。

var addresses = new Address[10];
addresses[0] = new Address { FirstName = "John", LastName = "Doe", ... };
Console.WriteLine("First name = {0}", addresses[0].FirstName;

A 2-dimensional array for this purpose seems very weird to me. 为此目的,二维数组对我来说很奇怪。 Are you really learning such things at the university? 您真的在大学里学习这些东西吗?

I pasted your code into my IDE, and the error that I see is " use of unassigned local variable ." 我将您的代码粘贴到IDE中,看到的错误是“ 使用未分配的局部变量”

You are seeing the error because you haven't initialized s1, s2, s3, s4 & s5 in a way that assures .Net that they will be given a value each time. 您看到此错误是因为您没有以确保.Net每次都会给它们赋值的方式初始化s1,s2,s3,s4和s5。

for (int i = 0; i < a.Length / 5; i++)
{
    s1 = a[i, 0];
    s2 = a[i, 1];
    s3 = a[i, 2];
    s4 = a[i, 3];
    s5 = a[i, 4];
}

The compiler sees that if a.Length is zero, these variables will not be assigned a value. 编译器看到,如果a.Length为零,则不会为这些变量分配值。 When you declare your variables, try initializing them to something first, like this: 在声明变量时,请尝试首先将它们初始化为某种形式,如下所示:

string s1 = System.String.Empty;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM