C#arraylist用户输入 [英] C# arraylist user input

查看:142
本文介绍了C#arraylist用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. But I would like to fill an ArrayList based on user input.

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

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.

这是我的代码:

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

I have a class Person which handles the user information:

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类:

And this is the Main class with main method:

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.

Is the thing I am trying to achieve even possible? Thank you for your answers. V.

推荐答案

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

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

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

From console program, you will use Console.ReadLine to get user input, validate/parse it and fill person instance and add to your list. 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);

...

这篇关于C#arraylist用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆