C# 数组长度问题的解决方案是什么
正如您在图中看到的,所有数组的长度都是[4],但即使我输入两个值来存储在数组中,也会出现此消息,这怎么可能,而数组大小是4 注意:numbers[]数组长度为[5]C# 数组长度问题的解决方案是什么,c#,arrays,size,C#,Arrays,Size,正如您在图中看到的,所有数组的长度都是[4],但即使我输入两个值来存储在数组中,也会出现此消息,这怎么可能,而数组大小是4 注意:numbers[]数组长度为[5] static void Main(字符串[]args) { //这是保存用户号码的主数组 字符串[]数字=新字符串[5]; Console.WriteLine(“输入5个电话号码”); for(int i=0;i
static void Main(字符串[]args)
{
//这是保存用户号码的主数组
字符串[]数字=新字符串[5];
Console.WriteLine(“输入5个电话号码”);
for(int i=0;i
将string[]stcNum=new string[4];
替换为List stcNum=new List();
和stcNum[i]=number[i];
替换为stcNum.Add(number[i]);
等
您还需要检查输入的电话号码是否超过2个字符,否则您的代码将再次被压碎。@HimBromBeere我这么做了,很抱歉这是我的第一次。
号码
是5个,其他是4个。-提示:当您可以使用列表时,请不要使用数组!您可能会发现OOP很有用:创建专用类型来保存对象I的属性与使用单独的数组并按索引映射它们相比,它更不容易出错。我有一种预感,您仍然相信存在某种神秘感,但您的循环尝试访问数组中只有4个元素长的第5个元素。
static void Main(string[] args)
{
//this is the main array to save user numbers
string[] numbers = new string[5];
Console.WriteLine("Enter 5 phone numbers");
for (int i = 0; i < numbers.Length; i++)
{
// user input
numbers[i] = Console.ReadLine();
}
phoneNumbers(numbers);
}
public static void phoneNumbers(string[] numbers)
{
// 4 arrays, for each array will store numbers if it belongs to STC,MOBILY,ZAIN or invaild
string[] stcNum = new string[4];
string[] mobilyNum = new string[4];
string[] zainNum = new string[4];
string[] invaildNum = new string[4];
for (int i = 0; i < numbers.Length; i++)
{
// an if statment to check if the number belong to STC, if yes, store it in STC array
if (numbers[i].Substring(0, 3).Equals("050") || numbers[i].Substring(0, 3).Equals("055"))
{
stcNum[i] = numbers[i];
}
// an if statment to check if the number belong to Mobily, if yes, store it in Mobily array
else if (numbers[i].Substring(0, 3).Equals("056") || numbers[i].Substring(0, 3).Equals("054"))
{
mobilyNum[i] = numbers[i];
}
// an if statment to check if the number belong to Zain, if yes, store it in Zain array
else if (numbers[i].Substring(0, 3).Equals("053") || numbers[i].Substring(0, 3).Equals("059"))
{
zainNum[i] = numbers[i];
}
// an if statment to check if the number is invalid, if yes, store it in invaild array
else if (!(numbers[i].Substring(0, 3).Equals("050") || numbers[i].Substring(0, 3).Equals("055") || numbers[i].Substring(0, 3).Equals("056") || numbers[i].Substring(0, 3).Equals("054") || numbers[i].Substring(0, 3).Equals("053") || numbers[i].Substring(0, 3).Equals("059")))
{
invaildNum[i] = numbers[i];
}
}