赔率/即使在多行文本框中VB.Net

时间:2019-04-17 14:41:10

标签: vb.net

如何在字符串中执行此操作(如果txtboxintdraws.lines(1)是值-2-3-4-6-7-8,则显示第一行的值, 在textbox1.Lines1()中显示2,4,6,8,在textbox2.text-3,7中显示。 (以逗号分隔)

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim number As Integer
        Dim numbers As New List(Of Integer)
        For Each strLine As String In TxtBoxIntDraws.Lines
            Dim nums() As String = strLine.Split(","c)
            For Each num As String In nums
                If Integer.TryParse(num.Trim, number) Then
                    numbers.Add(number)
                End If
            Next
        Next
        numbers.Sort()
        'When you are building strings use a StringBuilder to avoide creating and throwing away 
        'a bunch of strings. (happens every time you alter a string
        Dim TextBox1sb As New StringBuilder
        Dim TextBox2sb As New StringBuilder
        For Each intNumber As Integer In numbers
            'The Mod operator divides the first number by the second number
            'and returns the remainder
            If intNumber Mod 2 = 0 Then '(number / 2) = Int(number / 2) Then
                'Number is even
                TextBox1sb.AppendLine(intNumber.ToString)
                TextBox1.Text = TextBox1.Text + intNumber.ToString
            Else
                TextBox2sb.AppendLine(intNumber.ToString)
                TextBox2.Text = TextBox1.Text + intNumber.ToString
            End If
        Next
        'Update the UI only once, don't force a redraw on each iteration of the loop
    TextBox1.Text = TextBox1sb.ToString
    TextBox2.Text = TextBox2sb.ToString
    End Sub

它告诉我类型错误

2
2
2
2
4
4
4
6
6
6
6

那我哪里出错了?

3 个答案:

答案 0 :(得分:1)

如果要在每一行显示数字,则需要跟踪每一行以及保留两个单独的列表。一个用于赔率,另一个用于偶数。请参见下面的内容,以实现我为允许的快速实现。

Dim even As New Dictionary(Of Integer, String)
Dim odd As New Dictionary(Of Integer, String)
Dim currIndex As Integer = 0
Dim curNum As Integer = 0
Dim evenNumbers As String = String.Empty
Dim oddNumbers As String = String.Empty

' Go through all lines in the textbox
TxtBoxIntDraws.Lines.
            ToList().ForEach(Sub(line)
                                 evenNumbers = String.Empty
                                 oddNumbers = String.Empty
                                 ' Try and split out the numbers from the line
                                 line.Split({"-"c}, StringSplitOptions.RemoveEmptyEntries).AsEnumerable().
                                                                                                 Where(Function(i) Integer.TryParse(i.Trim, New Integer)).OrderBy(Function(num) CInt(num)).ToList().
                                                                                                 ForEach(Sub(l)                                                                    
                                                                              ' For each item if we can parse it, then check if even and or odd and add to the correct dictionary
                                                                                                           If Integer.TryParse(l.Trim, curNum) Then
                                                                                                                 If curNum Mod 2 = 0 Then
                                                                                                                     If evenNumbers.Length > 0 Then
                                                                                                                         evenNumbers += ", " & curNum.ToString
                                                                                                                     Else
                                                                                                                         evenNumbers += curNum.ToString
                                                                                                                     End If
                                                                                                                 Else
                                                                                                                     If oddNumbers.Length > 0 Then
                                                                                                                         oddNumbers += ", " & curNum.ToString
                                                                                                                     Else
                                                                                                                         oddNumbers += curNum.ToString
                                                                                                                     End If
                                                                                                                 End If
                                                                                                             End If
                                                                                                         End Sub)

                                 If Not String.IsNullOrEmpty(evenNumbers) Then even.Add(currIndex, currIndex & " - " & evenNumbers)
                                 If Not String.IsNullOrEmpty(oddNumbers) Then odd.Add(currIndex, currIndex & " - " & oddNumbers)
                                 currIndex += 1
                             End Sub)
' Show the results
TextBox1.Text = String.Join(Environment.NewLine, even.Values)
TextBox2.Text = String.Join(Environment.NewLine, odd.Values)

这是我的结果

enter image description here

答案 1 :(得分:0)

由于这是我上一个答案的代码,因此进行了一些修改可以解决问题。您可以修改最后的逗号。

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    TextBox3.Text = "-2-3-4-6-7-8" 'Pretend TextBox3 is TxtBoxIntDraws
    Dim number As Integer
    Dim numbers As New List(Of Integer)
    For Each strLine As String In TextBox3.Lines
        Dim nums() As String = strLine.Split("-"c)
        For Each num As String In nums
            If Integer.TryParse(num.Trim, number) Then
                numbers.Add(number)
            End If
        Next
    Next
    numbers.Sort()
    'When you are building strings use a StringBuilder to avoide creating and throwing away 
    'a bunch of strings. (happens every time you alter a string
    Dim TextBox1sb As New StringBuilder
    Dim TextBox2sb As New StringBuilder
    For Each intNumber As Integer In numbers
        'The Mod operator divides the first number by the second number
        'and returns the remainder
        If intNumber Mod 2 = 0 Then '(number / 2) = Int(number / 2) Then
            'Number is even
            TextBox1sb.Append(intNumber.ToString & ",")
            TextBox1.Text = TextBox1.Text + intNumber.ToString
        Else
            TextBox2sb.Append(intNumber.ToString & ",")
            TextBox2.Text = TextBox1.Text + intNumber.ToString
        End If
    Next
    'Update the UI only once, don't force a redraw on each iteration of the loop
    TextBox1.Text = TextBox1sb.ToString
    TextBox2.Text = TextBox2sb.ToString
End Sub

答案 2 :(得分:-1)

我在上面的代码中做了一些修改,并且测试正确,这里是您认为的要求,尤其是对于第一行,否则请根据需要进行自定义。这是更新代码:

int main(void) 
{ 
        struct dirent *de; /* Pointer for directory entry */

        /* opendir() returns a pointer of DIR type. */ 
        DIR *dr = opendir("."); 

        if (dr == NULL) /* opendir returns NULL if couldn't open directory */
        { 
                printf("Could not open current directory" ); 
                return 0; 
        } 

        /* Refer http://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html 
          for readdir() */
        while ((de = readdir(dr)) != NULL) 
                        printf("%s\n", de->d_name); 

        closedir(dr);    
        return 0; 
}

结束子

希望这对您有帮助,^ _ ^

enter image description here