如何在类中放置数组信息

时间:2017-09-28 11:21:13

标签: c# arrays class for-loop

我试过这样做,并从互联网上阅读如何使用课程,但我仍然不知道,如何为客户和航班信息定义单独的课程?

我制作了一个程序,用于从标准输入设备读取客户及其航班的信息,并允许搜索航班和客户信息。搜索航班信息时,应用程序应打印该航班上所有客户的信息。搜索客户信息时,应用程序也应打印客户航班的信息。

我需要为客户和航班信息定义单独的类。客户的类应包含姓名,ID和航班ID。该类还应定义构造函数和返回客户及其航班信息的方法。

航班类应包含身份证,来源,目的地和日期。以及构造函数和返回航班信息的方法,如果正确的航班ID传递给它。

提示:您需要定义客户和航班对象的数组,以使应用程序以有意义的方式工作。

string username;
Console.Write("Customer or Flight ? c= Customer  f= Flight ");
username = Console.ReadLine();

string[] array = new string[5];

Console.WriteLine("");
array[0] = "Customer";
array[1] = "Name";
array[2] = "Id";
array[3] = "Flight Id";

string[] flight = new string[6];
flight[0] = "Flight";
flight[1] = "Id";
flight[2] = "Origin";
flight[3] = "Destination";
flight[4] = "Date";

for (int i = 0; i < array.Length; i++)
{        
    if (username =="c")
    {
        string c = array[i];
        Console.WriteLine(c);
    }
    else if(username =="f")
    {
        string f = flight[i];
        Console.WriteLine(f);
    }
    else
    {
        Console.WriteLine();
    }
}

Console.ReadLine();

抱歉,我是c#的新手,我完成了剩下的工作,我只需要那部分。

1 个答案:

答案 0 :(得分:0)

我同意所有人的意见,你应该花几个小时阅读一本书或者观看一些关于复数或其他事物的例子。但是这里有一些代码可以帮助你入门。

在visual studio中创建一个新的Console C#项目。在解决方案资源管理器中单击您的项目,右键单击它并继续添加,将两个C#类文件添加到您的项目中。

FlightInformation.cs:

public class FlightInformation
{
    public FlightInformation()
    {
        Passengers = new List<Customers>();
    }

    public string Flight { get; set; }

    public int Id { get; set; }

    public string Origin { get; set; }

    public string Destination { get; set; }

    public DateTime Date { get; set; }

    public List<Customers> Passengers { get; set; }
}

Customers.cs:

public class Customers
{
    public string Customer { get; set; }

    public string Name { get; set; }

    public int Id { get; set; }

    public int FlightId { get; set; }
}

然后在你的Program.cs中开始添加客户/航班,然后再搜索代码 - 我不会为你做那部分,希望这会给你一个开始

class Program
{
   static void Main(string[] args)
   {
       var John = new Customers
       {
           Name = "John",
           FlightId = 24
       };

       var Flight = new FlightInformation
       {
           Id = 24,
           Destination="Cyprus"
       };

       Flight.Passengers.Add(John);

       //Code to search for flights

       //code to search for passengers
    }
}