Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 查找数组中的连续数字_Vb.net_Visual Studio_Visual Studio 2008 - Fatal编程技术网

Vb.net 查找数组中的连续数字

Vb.net 查找数组中的连续数字,vb.net,visual-studio,visual-studio-2008,Vb.net,Visual Studio,Visual Studio 2008,我需要在数组中找到连续的数字,并返回一个字符串,该字符串告诉范围和不构成范围的数字 我发现了一些已经提出的问题,但没有一个在VB.Net中: 如果数字数组看起来像{11,12,67,68,69,70,92,97},那么返回的字符串的形式应该是11,12,67到70,92和97 这不是家庭作业;对于包含统计数据的word文档,我需要此函数。直接输入回复窗口,因此几乎可以肯定存在一两个错误: Public Class Range Public Shared Function PrintR

我需要在数组中找到连续的数字,并返回一个字符串,该字符串告诉范围和不构成范围的数字

我发现了一些已经提出的问题,但没有一个在VB.Net中:

如果数字数组看起来像
{11,12,67,68,69,70,92,97}
,那么返回的字符串的形式应该是
11,12,67到70,92和97


这不是家庭作业;对于包含统计数据的word文档,我需要此函数。

直接输入回复窗口,因此几乎可以肯定存在一两个错误:

Public Class Range

    Public Shared Function PrintRanges(ByVal numbers() As Integer) As String
        Dim buffer As New List(Of Range)()
        Dim CurrentRange As Range = Nothing

        For Each i As Integer in numbers ' you may want to add a .OrderBy() here
            If CurrentRange IsNot Nothing AndAlso i - 1 = CurrentRange.EndValue Then
                 CurrentRange.Increase()
            Else
                CurrentRange = New Range(i)
                buffer.Add(CurrentRange)
            End If
        Next i

        'Got a little lazy for this line - it still does a ", " rather than " and " for the final delimiter. Simple code to fix it, just tedious.
        Return String.Join(", ", buffer.Select(Function(r) r.ToString()).ToArray())
    End Function

    Private Sub New(ByVal InitialValue As Integer)
        EndValue = IntialValue
        Length = 1
    End Sub

    'For completeness, these two properties should be made read only outside the class, but the private constructor makes that largely moot
    Public Property EndValue As Integer
    Public Property Length As Integer

    Public Sub Increase()
         Length += 1
         EndValue += 1
    End Sub

    Public Overrides Function ToString() As String
        If Length == 1 Then Return EndValue.ToString()
        If Length == 2 Then Return (EndValue -1).ToString() & "," & LastValue.ToString()
        Return (EndValue - Length).ToString() & " through " & EndValue.ToString()
    End Function

End Class