我发现最接近回答这个问题的是使用 for 循环将几个字符串输入转换为一个数组。
我只想取 1 个字符串并将前 7 位数字转换为一个 int 数组。
此代码采用字符的整数值,然后根据 Unicode 值测试它们,如果有效则返回 true,否则返回 false 以重复 while 循环并再次请求输入。当我使用 Console.Read(); 执行此操作时方法,并首先输入一个无效值,它会说我的代码在 7 次以上的迭代中无效。这意味着即使字符串具有有效输入,console.Read() 也必须再运行 7 次。
public static void GetDigits(ref int[] callNumberArray, ref bool valid)
{
Console.WriteLine("Please enter the code you wish to dial.");
while ( valid == false)
{//This loop will reiterate the read() function if the code is not valid.
valid = true;
for (int i = 0; i < 7; i++ )
{
if (i != 6 && i!= 5 && i != 5 && i != 4 && i != 3 && i != 2 && i != 1 && i != 0)
{
i = 0;
}
callNumberArray[i] = Console.Read();// I want to change this
}
for (int i = 0; i < 7; i++)
{
if (i != 6 && i != 5 && i != 5 && i != 4 && i != 3 && i != 2 && i != 1 && i != 0)
{
i = 0;
}
if (callNumberArray[0] == 53)
{
valid = false;
}
if (callNumberArray[i] < 49)
{
valid = false;
}
if (callNumberArray[i] > 57 && callNumberArray[i] < 65)
{
valid = false;
}
if (callNumberArray[i] > 90 && callNumberArray[i] < 97)
{
valid = false;
}
if (callNumberArray[i] > 122)
{
valid = false;
}
}
if (valid == false)
{
Console.WriteLine("You entered an invalid code. Please re-enter your code.");
}
}
我认为你应该使用正则表达式,例如:
MatchCollection matchList = Regex.Matches(Content, Pattern);
var list = matchList.Cast<Match>().Select(match => match.Value).ToList();
我是一名优秀的程序员,十分优秀!