Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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#_Arrays - Fatal编程技术网

C# 问:;带数组的程序

C# 问:;带数组的程序,c#,arrays,C#,Arrays,嗨,我目前正在学习C#,想知道如何为下面的问答创建一个数组 这是一个简单的问答程序,我试图写它来提高我的C#技能。谢谢 Console.WriteLine("What's your favorite baseball team? "); string baseball = Console.ReadLine(); Console.WriteLine("How old are you? "); string age = Console.ReadLi

嗨,我目前正在学习C#,想知道如何为下面的问答创建一个数组

这是一个简单的问答程序,我试图写它来提高我的C#技能。谢谢

 Console.WriteLine("What's your favorite baseball team? ");
        string baseball = Console.ReadLine();

        Console.WriteLine("How old are you? ");
        string age = Console.ReadLine();

        Console.WriteLine("Where do you live? ");
        string home = Console.ReadLine();

        Console.WriteLine("Aside from Baseball, what other sports do you love? ");
        string sports = Console.ReadLine();
    } 

您可以在数组初始化时将这些项添加到数组中,如下所示:

string[] myArray = {baseball, age, home, sports};
string[] myArray = new string[4];
myArray[0] = baseball;
myArray[1] = age;
myArray[2] = home;
myArray[3] = sports;
List<string> myList =  new List<string> { baseball, age, home, sports };
List<string> myList =  new List<string>();
myList.Add(baseball);
myList.Add(age);
myList.Add(home);
myList.Add(sports);
或者,您可以初始化数组,然后向其中添加项,如下所示:

string[] myArray = {baseball, age, home, sports};
string[] myArray = new string[4];
myArray[0] = baseball;
myArray[1] = age;
myArray[2] = home;
myArray[3] = sports;
List<string> myList =  new List<string> { baseball, age, home, sports };
List<string> myList =  new List<string>();
myList.Add(baseball);
myList.Add(age);
myList.Add(home);
myList.Add(sports);
但是,正如上面的回答所述,最好使用一个通用列表,该列表将根据您的需要进行扩展(初始化时不必指明元素的最大数量),如下所示:

string[] myArray = {baseball, age, home, sports};
string[] myArray = new string[4];
myArray[0] = baseball;
myArray[1] = age;
myArray[2] = home;
myArray[3] = sports;
List<string> myList =  new List<string> { baseball, age, home, sports };
List<string> myList =  new List<string>();
myList.Add(baseball);
myList.Add(age);
myList.Add(home);
myList.Add(sports);

此外,常规集合速度更快。

您可以在初始化时将项添加到数组中,如下所示:

string[] myArray = {baseball, age, home, sports};
string[] myArray = new string[4];
myArray[0] = baseball;
myArray[1] = age;
myArray[2] = home;
myArray[3] = sports;
List<string> myList =  new List<string> { baseball, age, home, sports };
List<string> myList =  new List<string>();
myList.Add(baseball);
myList.Add(age);
myList.Add(home);
myList.Add(sports);
或者,您可以初始化数组,然后向其中添加项,如下所示:

string[] myArray = {baseball, age, home, sports};
string[] myArray = new string[4];
myArray[0] = baseball;
myArray[1] = age;
myArray[2] = home;
myArray[3] = sports;
List<string> myList =  new List<string> { baseball, age, home, sports };
List<string> myList =  new List<string>();
myList.Add(baseball);
myList.Add(age);
myList.Add(home);
myList.Add(sports);
但是,正如上面的回答所述,最好使用一个通用列表,该列表将根据您的需要进行扩展(初始化时不必指明元素的最大数量),如下所示:

string[] myArray = {baseball, age, home, sports};
string[] myArray = new string[4];
myArray[0] = baseball;
myArray[1] = age;
myArray[2] = home;
myArray[3] = sports;
List<string> myList =  new List<string> { baseball, age, home, sports };
List<string> myList =  new List<string>();
myList.Add(baseball);
myList.Add(age);
myList.Add(home);
myList.Add(sports);

此外,泛型集合速度更快。

我假设您只是想将这些操作的结果存储在一个数组中。如果是这种情况,那么您只需实例化一个新数组,该数组将保存您的每个响应:

string[] answers = new string[3];

Console.WriteLine("What's your favorite baseball team? ");
string baseball = Console.ReadLine();
// Store your first answer in the first index of the array
answers[0] = baseball;

Console.WriteLine("How old are you? ");
string age = Console.ReadLine();
// Store your age in the second index, etc.
answers[1] = age;

// Continue with your other questions here
或者,您也可以在拥有所有变量后,在末尾构建数组:

string[] answers = { baseball, age, ... };
然后可以通过数组中的索引引用这些值:

string sentence = "I am " + answers[1] + " years old";

如果你需要将你的项目存储在一个可以动态地适应你的需求的集合中,你可以考虑使用A,同样地,如果你需要用他们的名字来引用你的值(例如“棒球”,“年龄”等),那么你可以使用.

,我假设你只想把这些操作的结果存储在一个数组中。如果是这种情况,那么您只需实例化一个新数组,该数组将保存您的每个响应:

string[] answers = new string[3];

Console.WriteLine("What's your favorite baseball team? ");
string baseball = Console.ReadLine();
// Store your first answer in the first index of the array
answers[0] = baseball;

Console.WriteLine("How old are you? ");
string age = Console.ReadLine();
// Store your age in the second index, etc.
answers[1] = age;

// Continue with your other questions here
或者,您也可以在拥有所有变量后,在末尾构建数组:

string[] answers = { baseball, age, ... };
然后可以通过数组中的索引引用这些值:

string sentence = "I am " + answers[1] + " years old";

如果你需要将你的项目存储在一个可以动态地适应你的需求的集合中,你可以考虑使用A,同样地,如果你需要用他们的名字来引用你的价值(例如“棒球”,“年龄”等),那么你可以使用.

你能给我们一点指导,指导你想要达到的目标吗?我们应该如何使用你在数组中发布的代码?你们有什么需要帮助的地方吗?你们能不能给我们一些指导,告诉我们你们想要达到的目标?我们应该如何使用你在数组中发布的代码?你们有什么需要帮助的吗?