我有3个方法1。接受字符串作为数组[zipCodes], 2。输出用户和3的菜单。将字符串数组显示给用户。前两个选项正在工作,经过测试,我可以说数组正在工作并接受字符串,但是我有麻烦将它们显示给用户。
我在整型中使用了这个方法,它使我认为[I]只用于1个字符,如果能解释一下,我将不胜感激。
//这是目前为止的代码
static void Main(string[] args)
{
string[] zipCodes = new string[10];
string zCounter;
for (int i = 0; i < zipCodes.Length; i++)
{
Console.WriteLine("Please enter 10 destinations:");
zCounter = Convert.ToString(Console.ReadLine());
zCounter = zipCodes[i];
}
int sentinalNo;
Console.Clear();
Console.WriteLine("Please enter from the following options: ");
Console.WriteLine("1. Display order zipcodes.");
Console.WriteLine("2. Search zipcode.");
Console.WriteLine("3. Exit.");
sentinalNo = Convert.ToInt32(Console.ReadLine());
while (sentinalNo != 3)
{
switch (sentinalNo)
{
case 1:
DisplayZips(zipCodes);
break;
}
}
}
private static void DisplayZips(string[] zipCodes)
{
for (int i = 0; i < zipCodes.Length; i++)
{
// Why doesnt this work?
Console.WriteLine(zipCodes[i]);
}
你应该分配输入到数组项:
// array of 10 strings each of them is null
string[] zipCodes = new string[10];
...
for (int i = 0; i < zipCodes.Length; i++)
{
Console.WriteLine("Please enter 10 destinations:");
// Convert.ToString is redundant here
zCounter = Convert.ToString(Console.ReadLine());
// swapped: user input is assigned to array items
zipCodes[i] = zCounter;
}