Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 每次调用方法时向方法中的数组添加值_C# - Fatal编程技术网

C# 每次调用方法时向方法中的数组添加值

C# 每次调用方法时向方法中的数组添加值,c#,C#,我有一个叫做乘客的数组,假设它有10个位置。我还有一个新乘客的课程,有乘客的年龄。 我想按他的年龄向阵列中添加一名新乘客。该乘客将通过调用构造函数由其类创建。 假设我通过调用该方法只添加一个乘客,9个位置为空。下次调用该方法时,我想在乘客数组的下一个位置添加一个新乘客! 所以每次我想添加一个新的乘客,我调用乘客方法,它将保存在数组中的下一个空位置,直到它满为止。一条信息会说已经客满,乘客不能继续 我的密码问题是我必须一次输入所有乘客!但每次调用该方法时,我只想输入一名乘客 public void

我有一个叫做乘客的数组,假设它有10个位置。我还有一个新乘客的课程,有乘客的年龄。 我想按他的年龄向阵列中添加一名新乘客。该乘客将通过调用构造函数由其类创建。 假设我通过调用该方法只添加一个乘客,9个位置为空。下次调用该方法时,我想在乘客数组的下一个位置添加一个新乘客! 所以每次我想添加一个新的乘客,我调用乘客方法,它将保存在数组中的下一个空位置,直到它满为止。一条信息会说已经客满,乘客不能继续

我的密码问题是我必须一次输入所有乘客!但每次调用该方法时,我只想输入一名乘客

public void Add_Passengers ()
    {
        Console.WriteLine("How old are the passengers?");

        for (int i = 0; i < passengers.Length; i++)
        {
            int age = Int32.Parse(Console.ReadLine());
            passengers[i] = new Passenger(age);
        }
    }
public void添加乘客()
{
控制台。WriteLine(“乘客几岁?”);
对于(int i=0;i
您可以尝试:

    public void AddPassengers(Passenger[] passengers)
    {
        int i = Array.IndexOf(passengers, null);

        if (i < 0)
        {
            Console.WriteLine("The array is full.");
            return;
        }

        Console.WriteLine("How old is the passenger?");

        int age = Int32.Parse(Console.ReadLine());
        passengers[i] = new Passenger(age);
    }
公共乘客(乘客[]乘客)
{
int i=Array.IndexOf(乘客,空);
if(i<0)
{
WriteLine(“数组已满”);
返回;
}
控制台。WriteLine(“乘客多大岁数?”);
int age=Int32.Parse(Console.ReadLine());
乘客[i]=新乘客(年龄);
}
希望这有帮助。

您可以尝试:

    public void AddPassengers(Passenger[] passengers)
    {
        int i = Array.IndexOf(passengers, null);

        if (i < 0)
        {
            Console.WriteLine("The array is full.");
            return;
        }

        Console.WriteLine("How old is the passenger?");

        int age = Int32.Parse(Console.ReadLine());
        passengers[i] = new Passenger(age);
    }
public void Add_Passengers()
    {
        PassengerClass PassengerClass = new PassengerClass();

        Console.WriteLine("How old are the passengers?");

        PassengerClass.AddPassanger(Int32.Parse(Console.ReadLine()));
    }

    public class PassengerClass
    {
        public List<int> passangerList;
        public PassengerClass()
        {
            if (passangerList == null)
                passangerList = new List<int>();
        }

        public void AddPassanger(int age)
        {
            passangerList.Add(age);
        }
    }
公共乘客(乘客[]乘客)
{
int i=Array.IndexOf(乘客,空);
if(i<0)
{
WriteLine(“数组已满”);
返回;
}
控制台。WriteLine(“乘客多大岁数?”);
int age=Int32.Parse(Console.ReadLine());
乘客[i]=新乘客(年龄);
}
希望这能有所帮助。

公共空间添加乘客()
public void Add_Passengers()
    {
        PassengerClass PassengerClass = new PassengerClass();

        Console.WriteLine("How old are the passengers?");

        PassengerClass.AddPassanger(Int32.Parse(Console.ReadLine()));
    }

    public class PassengerClass
    {
        public List<int> passangerList;
        public PassengerClass()
        {
            if (passangerList == null)
                passangerList = new List<int>();
        }

        public void AddPassanger(int age)
        {
            passangerList.Add(age);
        }
    }
{ PassengerClass PassengerClass=新的PassengerClass(); 控制台。WriteLine(“乘客几岁?”); AddPassanger(Int32.Parse(Console.ReadLine()); } 公务舱乘客舱 { 公众名单乘客名单; 公共乘客类() { if(passangerList==null) PassengerList=新列表(); } 公共空乘客(国际年龄) { 乘客名单。添加(年龄); } }
公共无效添加乘客()
{
PassengerClass PassengerClass=新的PassengerClass();
控制台。WriteLine(“乘客几岁?”);
AddPassanger(Int32.Parse(Console.ReadLine());
}
公务舱乘客舱
{
公众名单乘客名单;
公共乘客类()
{
if(passangerList==null)
PassengerList=新列表();
}
公共空乘客(国际年龄)
{
乘客名单。添加(年龄);
}
}
//
///乘客阵列
/// 
公共乘客[]乘客=新乘客[10];
公务舱乘客
{
公共整数{get;set;}
公共乘客(国际年龄)
{
年龄=年龄;
}
}
公共交通乘客()
{
//了解乘客人数
int passengerCount=passers.Count(p=>p!=null);
if(乘客计数==乘客长度)
控制台。WriteLine(“最大乘客数”);
其他的
{
控制台。WriteLine(“乘客几岁?”);
int age=int.Parse(Console.ReadLine());
//增加乘客
乘客【乘客人数】=新乘客(年龄);
}
}
//
///乘客阵列
/// 
公共乘客[]乘客=新乘客[10];
公务舱乘客
{
公共整数{get;set;}
公共乘客(国际年龄)
{
年龄=年龄;
}
}
公共交通乘客()
{
//了解乘客人数
int passengerCount=passers.Count(p=>p!=null);
if(乘客计数==乘客长度)
控制台。WriteLine(“最大乘客数”);
其他的
{
控制台。WriteLine(“乘客几岁?”);
int age=int.Parse(Console.ReadLine());
//增加乘客
乘客【乘客人数】=新乘客(年龄);
}
}

我会选择课堂解决方案:

公共舱乘客{
公共信息;
公共乘客(国际年龄)
{
这个。年龄=年龄;
}
}
公务舱旅客
{
私人旅客名单;
私有整数限制;
公共布尔就绪{get;private set;}
公共乘客(国际通行能力)
{
乘客=新名单(容量);
极限=容量;
就绪=错误;
}
公共乘客(乘客乘客)
{
乘客。添加(乘客);
if(passangers.Count==限制)
{
就绪=正确;
}
}
公共列表GetPassenger()
{
旅客。排序((p1,p2)=>p1.年龄。与(p2.年龄)相比;
返程旅客;
}
}

我会选择课堂解决方案:

公共舱乘客{
公共信息;
公共乘客(国际年龄)
{
这个。年龄=年龄;
}
}
公务舱旅客
{
私人旅客名单;
私有整数限制;
公共布尔就绪{get;private set;}
公共乘客(国际通行能力)
{
乘客=新名单(容量);
限制=上限
private Passenger[] passengers = new Passenger[10];
private int _count = 0;
public void Add_Passengers()
{
    // validate overflow. >= not strictly necessary but a better safeguard than ==
    if (_count >= passengers.Length)
    {
       Console.WriteLine("Error: No more passengers are allowed. Size limit reached.");
       return;
    } 
    
    Console.WriteLine("How old are the passengers?");
    
    // validate input
    if (!int.TryParse(Console.ReadLine(), out int age)) 
    {
       Console.WriteLine("Error: Value is not an integer");
       return;
    } 

    // Postfix increment returns original value 
    passengers[_count++] = new Passenger(age);
    
}