繁体   English   中英

C#arraylist用户输入

[英]C# arraylist user input

我在C#中遇到ArrayList的问题,我知道如何添加自己的输入并运行程序,以便Array充满信息。 但我想根据用户输入填充ArrayList。

这就是我需要的:用户可以输入他/她的姓名,生日和年龄。 并且所有这些三个信息将存储在一个元素中。

我的目标是能够创建一个应用程序,该应用程序允许用户为几个人输入这种数据,然后打印输出。

这是我的代码:

我有一个Person类来处理用户信息:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArrayList_Siomple_Sorted_10
{
    class Person
    {
    private string personName;
    private DateTime birthDate;
    private double personAge;

    public Person(string name, DateTime bDate, double age)
    {
        personName = name;
        birthDate = bDate;
        personAge = age;
    }
    public string Name
    {
        get { return personName; }
        set { personName = value; }
    }
    public DateTime Birthdate
    {
        get { return birthDate; }
        set { birthDate = value; }
    }

    public double Grade
    {
        get { return personAge; }
        set { personAge = value; }
    }

    public void Show()
    {
        Console.WriteLine(personName + " " + birthDate.ToString("d.M.yyyy") + " " + personAge);
    }
}
}

这是具有main方法的Main类:

using System;
using System.Collections;
using System.Text;

namespace ArrayList_Siomple_Sorted_10
{
class Program
{
    static void Main(string[] args)
    {
        DateTime birthDateP3 = new DateTime(1980, 2, 25);
        Person p3 = new Person("Ann Person", birthDateP3, 8);
        DateTime birthDateP2 = new DateTime(1980, 2, 25);
        Person p2 = new Person("Ann Person", birthDateP2, 8);
        DateTime birthDateP1 = new DateTime(1980, 2, 25);
        Person p1 = new Person("Ann Person", birthDateP1, 8);

        ArrayList ar = new ArrayList();

        ar.Add(p1);
        ar.Add(p2);
        ar.Add(p3);
        Console.WriteLine("Print the original Array");
        foreach (Person pr in ar)
            pr.Show();

    }
}
}

我试图实现的目标是否可能? 谢谢您的回答。 V.

是的-有可能。 您在这里面临的确切问题是什么? 顺便说一句,您应该使用通用集合List<Person>而不是ArrayList

在控制台程序中,您将使用Console.ReadLine来获取用户输入,验证/解析它并填写个人实例并添加到您的列表中。 例如,

...
    var ar = new List<Person>();

    var name = Console.ReadLine();
    // validate name (check if its not blank string etc)
    ...

    var dob = Console.ReadLine();
    // validate date of birth (date/time format, past date etc)
    ...
    DateTime dateOfBirth = DateTime.Parse(dob);

    // compute age
    var age = (DateTime.Now - dateOfBirth).Years;

    var p = new Person(name, dateOfBirth, age);
    ar.Add(p);

...

首先,您应该考虑使用List<T>而不是ArrayList。

其次,这当然是可能的。 您可以按照以下方式进行操作。

static void Main(string[] args)
{
    DateTime birthDateP3 = new DateTime(1980, 2, 25);
    Person p3 = new Person("Ann Person", birthDateP3, 8);
    DateTime birthDateP2 = new DateTime(1980, 2, 25);
    Person p2 = new Person("Ann Person", birthDateP2, 8);
    DateTime birthDateP1 = new DateTime(1980, 2, 25);
    Person p1 = new Person("Ann Person", birthDateP1, 8);

    ArrayList ar = new ArrayList();

    string name = Console.ReadLine();        
    ar.Add(name);


    Console.WriteLine("Print the original Array");
    foreach (Person pr in ar)
        pr.Show();

}

并且您应该使用通用列表而不是ArrayList如下所示:

List<string> mylist = new List<string>();
string str = Console.ReadLine();
mylist.Add(str);

当然,您可以使用Console.ReadLine()方法读取输入数据,或者,如果要使用更复杂的界面,可以使用Windows Form API。

暂无
暂无

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

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