遍历列表以检查数字是否连续
Iterating over a list to check if numbers are consecutive
代码的目标是获取一串由连字符分隔的数字,并检查它们是否连续。
我尝试使用 string.spit,遍历数组并检查数字 +1 是否等于下一个值。但是我 运行 遇到了我正在处理的索引超出数组边界的问题。
这是当前版本。
public static void Consecutive()
{
Console.Write("enter multiple numbers separated by hyphens: ");
var userinput = Console.ReadLine();
var nums = userinput.Split();
int index = 0;
bool isConsecutive = false;
foreach(var number in nums)
{
if(Convert.ToInt32(nums[index + 1]) == Convert.ToInt32(nums[index] + 1))
{
isConsecutive = true;
}
else
{
isConsecutive = false;
}
if(index == nums.Length)
{
break;
}
}
Console.WriteLine(isConsecutive);
}
这是因为当 foreach
循环遍历最后一个元素时,没有包含 index + 1
的元素,并且您的检查 if(index == nums.Length)
不起作用。
解释:
nums.Length = 最后一个索引 + 1.
对于包含 5 个元素的数组,长度 = 5,最后一个索引 = 4。
因为你的 if 条件永远不会 true
.
解法:
将条件从 if(index == nums.Length)
更改为 if(index == nums.Length - 1)
您想按连字符拆分,但您按 white-spaces 拆分。您也不要使用 foreach
的 number
循环变量。对于你正在做的事情,for
-loop 无论如何更合适:
public static void Consecutive()
{
Console.WriteLine("enter multiple numbers separated by hyphens: ");
string userinput = Console.ReadLine();
string[] nums = userinput.Split('-');
// always validate input
while (!nums.All(n => n.All(char.IsDigit)))
{
Console.WriteLine("All numbers must be valid integers");
userinput = Console.ReadLine();
nums = userinput.Split('-');
}
bool isConsecutive = true;
for(int i = 1; i < nums.Length; i++)
{
int thisInt = int.Parse(nums[i]);
int prevInt = int.Parse(nums[i - 1]);
if(thisInt != prevInt + 1)
{
isConsecutive = false;
break;
}
}
Console.WriteLine(isConsecutive);
}
这是一种不同的方法,生成预期的序列并查看它是否与清理后的输入匹配:
var delimiter = ",";
Console.Write("Enter multiple integers separated by '" + delimiter + "': ");
var userinput = Console.ReadLine(); ;
bool isConsecutive = false;
try
{
var strNums = userinput.Split(delimiter.ToCharArray());
var nums = Array.ConvertAll(strNums, x => int.Parse(x));
var consecutive = Enumerable.Range(nums[0], nums.Length);
var strCleansed = String.Join(delimiter, nums);
var strGenerated = String.Join(delimiter, consecutive);
isConsecutive = (strCleansed == strGenerated);
Console.WriteLine(" cleansed = " + strCleansed);
Console.WriteLine(" generated = " + strGenerated);
}
catch (Exception)
{
Console.WriteLine("Invalid Integer in Input");
}
Console.WriteLine("isConsecutive = " + isConsecutive);
Console.ReadKey();
样本运行:
Enter multiple integers separated by ',': -3, -0000002, -1, 0, 1, 02, 3
cleansed = -3,-2,-1,0,1,2,3
generated = -3,-2,-1,0,1,2,3
isConsecutive = True
Enter multiple integers separated by ',': 1,2,3,5
cleansed = 1,2,3,5
generated = 1,2,3,4
isConsecutive = False
Enter multiple integers separated by ',': 1,fish,2,fish,red fish,blue fish
Invalid Integer in Input
isConsecutive = False
代码的目标是获取一串由连字符分隔的数字,并检查它们是否连续。 我尝试使用 string.spit,遍历数组并检查数字 +1 是否等于下一个值。但是我 运行 遇到了我正在处理的索引超出数组边界的问题。 这是当前版本。
public static void Consecutive()
{
Console.Write("enter multiple numbers separated by hyphens: ");
var userinput = Console.ReadLine();
var nums = userinput.Split();
int index = 0;
bool isConsecutive = false;
foreach(var number in nums)
{
if(Convert.ToInt32(nums[index + 1]) == Convert.ToInt32(nums[index] + 1))
{
isConsecutive = true;
}
else
{
isConsecutive = false;
}
if(index == nums.Length)
{
break;
}
}
Console.WriteLine(isConsecutive);
}
这是因为当 foreach
循环遍历最后一个元素时,没有包含 index + 1
的元素,并且您的检查 if(index == nums.Length)
不起作用。
解释:
nums.Length = 最后一个索引 + 1.
对于包含 5 个元素的数组,长度 = 5,最后一个索引 = 4。
因为你的 if 条件永远不会 true
.
解法:
将条件从 if(index == nums.Length)
更改为 if(index == nums.Length - 1)
您想按连字符拆分,但您按 white-spaces 拆分。您也不要使用 foreach
的 number
循环变量。对于你正在做的事情,for
-loop 无论如何更合适:
public static void Consecutive()
{
Console.WriteLine("enter multiple numbers separated by hyphens: ");
string userinput = Console.ReadLine();
string[] nums = userinput.Split('-');
// always validate input
while (!nums.All(n => n.All(char.IsDigit)))
{
Console.WriteLine("All numbers must be valid integers");
userinput = Console.ReadLine();
nums = userinput.Split('-');
}
bool isConsecutive = true;
for(int i = 1; i < nums.Length; i++)
{
int thisInt = int.Parse(nums[i]);
int prevInt = int.Parse(nums[i - 1]);
if(thisInt != prevInt + 1)
{
isConsecutive = false;
break;
}
}
Console.WriteLine(isConsecutive);
}
这是一种不同的方法,生成预期的序列并查看它是否与清理后的输入匹配:
var delimiter = ",";
Console.Write("Enter multiple integers separated by '" + delimiter + "': ");
var userinput = Console.ReadLine(); ;
bool isConsecutive = false;
try
{
var strNums = userinput.Split(delimiter.ToCharArray());
var nums = Array.ConvertAll(strNums, x => int.Parse(x));
var consecutive = Enumerable.Range(nums[0], nums.Length);
var strCleansed = String.Join(delimiter, nums);
var strGenerated = String.Join(delimiter, consecutive);
isConsecutive = (strCleansed == strGenerated);
Console.WriteLine(" cleansed = " + strCleansed);
Console.WriteLine(" generated = " + strGenerated);
}
catch (Exception)
{
Console.WriteLine("Invalid Integer in Input");
}
Console.WriteLine("isConsecutive = " + isConsecutive);
Console.ReadKey();
样本运行:
Enter multiple integers separated by ',': -3, -0000002, -1, 0, 1, 02, 3
cleansed = -3,-2,-1,0,1,2,3
generated = -3,-2,-1,0,1,2,3
isConsecutive = True
Enter multiple integers separated by ',': 1,2,3,5
cleansed = 1,2,3,5
generated = 1,2,3,4
isConsecutive = False
Enter multiple integers separated by ',': 1,fish,2,fish,red fish,blue fish
Invalid Integer in Input
isConsecutive = False