gpt4 book ai didi

c# - 如何在 C# 中将用户输入转换为 int 数组

转载 作者:太空宇宙 更新时间:2023-11-03 18:26:13 25 4
gpt4 key购买 nike

我发现最接近回答这个问题的是使用 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();

关于c# - 如何在 C# 中将用户输入转换为 int 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33854151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com