Dim Strs() As Integer = New Integer() {1, 2, 3, 4, 6, 7, 9, 10, 11, 14, 15, 16, 18, 19, 20}
Dim intCur As Integer '当前数值
Dim IntPrew As Integer
Dim IntRang As Integer '一共找到多少段
Dim intS() As Integer = New Integer() {-1, -1, -1, -1, -1, -1, -1} '保存段的开始数字
Dim intE() As Integer = New Integer() {-1, -1, -1, -1, -1, -1, -1} '保存段的结束数字
For i As Integer = 0 To Strs.Length - 2
intCur = Strs(i)
IntPrew = Strs(i + 1)
If IntPrew - intCur = 1 Then
If intS(IntRang) = -1 Then
intS(IntRang) = intCur
End If
If i = Strs.Length - 2 Then
'如果到了倒数第二数字,最后一个数字就是结束了
intE(IntRang) = IntPrew
End If
Else
intE(IntRang) = intCur '设置当前段的结束
IntRang += 1 '连续的数字段计数器+1
End If
Next
MsgBox(IntRang + 1)