我有7个数字值,零或更大,按一周中的每一天排序。只有一个或多个所有值可能大于零。我需要一种方法来确定> 0值是否出现在SINGLE序列中。
例如:
0, 8, 8, 0, 0, 0, 0
是
时的单个序列8, 0, 8, 8, 8, 0, 0
有两个独立的序列,因此不符合条件。如果整个分组中只有一个大于零的值,我也希望它被认为是一个合格的序列。
我已经开始使用一个函数,它接受7个变量并将它们填充到一个布尔数组中,基于每个变量是零还是非零 - 假为0为0而对于> 0为真,这给了我类似的东西
{false, true, true, false, false, false, false}
我现在需要确定该数组的内容是否包含符合上述条件的限定序列。有什么想法吗?
对于它的价值,这是我到目前为止的功能:
Public Function IsConcurrent(D1, D2, D3, D4, D5, D6, D7) As Boolean
Dim arr(6) As Single
arr(0) = D1
arr(1) = D2
arr(2) = D3
arr(3) = D4
arr(4) = D5
arr(5) = D6
arr(6) = D7
Dim concurrent As Boolean = False
If CSng(arr(0)) > 0 Then concurrent = True
For k = 2 To 7
If arr(k - 1) = arr(k - 2) Then
Else
End If
Next
Return concurrent
End Function
答案 0 :(得分:1)
...我用Lists
而不是数组写了这个,但结构是相同的。
我是这样做的:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim x As List(Of Integer) = {0, 0, 8, 8, 8, 0, 0}.ToList
Dim Test As Boolean = LookForSingleSequence(x)
End Sub
Private Function LookForSingleSequence(ByVal MyList As List(Of Integer)) As Boolean
Dim i As Integer
Dim RetVal As Boolean = False
Dim SeqEnded As Boolean = False
For i = 0 To MyList.Count - 1
If MyList(i) > 0 Then
If SeqEnded = True Then
RetVal = False
Exit For
Else
RetVal = True
End If
End If
If RetVal = True And MyList(i) <= 0 Then SeqEnded = True
Next
Return RetVal
End Function
希望这会有所帮助!!
答案 1 :(得分:1)
这是LINQ解决方案 - 更少的代码行,希望更容易理解:
Sub Main()
Dim lst As New List(Of Integer)({0, 8, 8, 0, 0, 0, 0})
Console.WriteLine(IsConcurrent(lst)) 'True
lst = New List(Of Integer)({8, 0, 8, 8, 8, 0, 0})
Console.WriteLine(IsConcurrent(lst)) 'False
Console.ReadLine()
End Sub
Private Function IsConcurrent(ByVal lst As List(Of Integer)) As Boolean
Dim elementsAfterZeros = lst.SkipWhile(Function(x) x <= 0)
If elementsAfterZeros.Count = 0 Then Return False 'we only have zeros
Dim elementsInSecondGroupOfNonZeros = elementsAfterZeros.
SkipWhile(Function(x) x > 0).
SkipWhile(Function(x) x <= 0)
If elementsInSecondGroupOfNonZeros.Count = 0 Then Return True
Return False
End Function