简体   繁体   English

C#arraylist用户输入

[英]C# arraylist user input

I have a problem with ArrayList in C#, I know how can I add my own input and run the program so the Array is filled with information. 我在C#中遇到ArrayList的问题,我知道如何添加自己的输入并运行程序,以便Array充满信息。 But I would like to fill an ArrayList based on user input. 但我想根据用户输入填充ArrayList。

This is what I need: the user could enter his/hers name, birthdate and age. 这就是我需要的:用户可以输入他/她的姓名,生日和年龄。 And all these theree information would be stored in one element. 并且所有这些三个信息将存储在一个元素中。

My goal is to be able to make an application which would allow the user to enter this kind of data for several people and then print the output. 我的目标是能够创建一个应用程序,该应用程序允许用户为几个人输入这种数据,然后打印输出。

Here is my code: 这是我的代码:

I have a class Person which handles the user information: 我有一个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);
    }
}
}

And this is the Main class with main method: 这是具有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();

    }
}
}

Is the thing I am trying to achieve even possible? 我试图实现的目标是否可能? Thank you for your answers. 谢谢您的回答。 V. V.

Yes - it's possible. 是的-有可能。 What is the exact issue that are you facing here? 您在这里面临的确切问题是什么? BTW, instead of ArrayList , you should be using generic collection List<Person> . 顺便说一句,您应该使用通用集合List<Person>而不是ArrayList

From console program, you will use Console.ReadLine to get user input, validate/parse it and fill person instance and add to your list. 在控制台程序中,您将使用Console.ReadLine来获取用户输入,验证/解析它并填写个人实例并添加到您的列表中。 For example, 例如,

...
    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);

...

First of all, you should consider using List<T> instead of ArrayList. 首先,您应该考虑使用List<T>而不是ArrayList。

Secondly, off course, this is possible. 其次,这当然是可能的。 You can do it in the following way. 您可以按照以下方式进行操作。

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();

}

And you should be using Generic list instead of ArrayList like this: 并且您应该使用通用列表而不是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