【问题标题】:Loop object creation循环对象创建
【发布时间】:2016-04-11 14:05:34
【问题描述】:

我需要让我的第二个嵌套 for 循环将数组值发送给班级朋友。 我不知道我会怎么做? 除非我在课堂上错过了什么?

namespace List
{
    class Program
    {
        public const int ARRAYSIZE = 5;
        static void Main()
        {
            string[] relSend = { "Enter  name", "enter phone number", "enter 2 didigt month dob", "enter 2 digit day dob", "enter 2 digit dob year" };
            string[] In = new string[5];
            string[] answer = new string[10];
            for (int x = 0; x <= 8; x++)
            {
                for (int i = 0; i < relSend.Length; i++)
                {
                    WriteLine("{0}", relSend[i]);
                    In[i] = Console.ReadLine();
                }

                for (int i = 0; i < In.Length; i++)
                {

                }
            }
        }
    }
}

public class Friends
{
    public string Name { get; set; }
    public int Phone { get; set; }
    public int Month { get; set; }
    public int Day { get; set; }
}

【问题讨论】:

  • 您似乎缺少代码的结尾?

标签: c# arrays oop


【解决方案1】:

我猜你的意思是你想从收集的信息中创建一个对象,这没问题:

List<Friend> friends = new List<Friend>();

for (int x = 0; x <= 8; x++)
{
    for (int i = 0; i < relSend.Length; i++)
    {
        WriteLine("{0}", relSend[i]);
        In[i] = Console.ReadLine();
    }

    friends.Add( new Friend() { Name = In[0]
                              , Phone = int.Parse(In[1])
                              , Month = int.Parse(In[2])
                              , Day = int.Parse(In[3])
                              , Year = int.Parse(In[4])
                              }
               );
}

确保在创建对象之前验证输入!另外,我建议使用string 作为电话号码,因为您会丢失通常的前缀0MonthDayYear 可能合并为一个 DateTime

【讨论】:

  • 这里要注意一件有趣的事情:他的班级被称为Friends(我也将其重命名为Friend)。目前,由于 Friends/Friend 命名差异,这不会编译。
  • 好的,因为我对命名约定有强迫症,我会保留它Friend ;) @Michael
  • 可能还想将List&lt;Friends&gt; friends = new List&lt;Friend&gt;(); 更新为List&lt;Friend&gt; friends = new List&lt;Friend&gt;(); 以避免对OP 造成混淆。
【解决方案2】:

如果您需要任何解释,请告诉我:

namespace List
{
    class Program
    {
        //Create a dictionary to find out each question is realated to which property.
        private static Dictionary<string, string> questions = new Dictionary<string, string>();
        static void Main()
        {
            questions.Add("Enter  name", "Name");
            questions.Add("enter phone number", "Phone");
            questions.Add("enter 2 didigt month dob", "Month");
            questions.Add("enter 2 digit day dob", "Day");
            questions.Add("enter 2 digit dob year", "Year");
            //Create list of friends
            List<Friends> friendsList = new List<Friends>();
            for (int x = 0; x <= 8; x++)
            {
                Friends f = new Friends();
                foreach (string q in questions.Keys)
                {
                    Console.WriteLine("{0}", q);
                    //Find property using Sytem.Reflection
                    System.Reflection.PropertyInfo property = f.GetType().GetProperty(questions[q]);
                    //Set value of found property
                    property.SetValue(f, Convert.ChangeType(Console.ReadLine(), property.PropertyType), null);
                }
                //Add a friend to list
                friendsList.Add(f);
            }
        }
    }
}

public class Friends
{
    public string Name { get; set; }
    public int Phone { get; set; }
    public int Year { get; set; }
    public int Month { get; set; }
    public int Day { get; set; }
}

【讨论】:

  • 是的,效果很好,但是我如何在这些对象中搜索名称并获取与该名称相关的信息?我是新来的课程,我会用 2 damensial 数组来做到这一点
猜你喜欢
  • 1970-01-01
  • 2012-11-29
  • 2015-02-23
  • 2016-11-11
  • 1970-01-01
  • 2020-08-08
  • 2015-02-04
  • 1970-01-01
相关资源
最近更新 更多