简体   繁体   English

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

[英]Odds / Even in a Multiline Textbox VB.Net

How do I do this in the string (to display the values of the first line if txtboxintdraws.lines (1) is the value - 2-3-4-6-7-8, in textbox1.Lines1 () to display 2,4,6,8, and in textbox2.text - 3,7. (separated by a comma) 我如何在字符串中执行此操作(如果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

It shows me wrong, genre 流派告诉我

2
2
2
2
4
4
4
6
6
6
6

So where do I get wrong? 那么我哪里出错了?

If you want your numbers to be shown for each corresponding line, you would need to keep track of each line as well as keeping two separate list's; 如果要在每一行显示数字,则需要跟踪每一行以及保留两个单独的列表。 one for odds and the other for even numbers. 一个用于赔率,另一个用于偶数。 Please see below for a quick implementation I did to allow for this. 请参阅下面的内容,我做了一个快速实现。

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)

Here are my results 这是我的结果

在此处输入图片说明

Since that was my code from a previous answer, a few edits fixes things. 由于那是我上一个答案的代码,因此进行了一些修改即可解决问题。 You can fix the final comma. 您可以解决最后的逗号。

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

I did a little bit modification on your code above and I tested it correctly, here you what I think you asked for, especially for the first line, otherwise do customize it as you want. 我在上面的代码上做了一些修改,并对其进行了正确的测试,这是我认为您要的内容,特别是对于第一行,否则请根据需要进行自定义。 Here is the update code: 这是更新代码:

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 TxtArr.Lines
    'Dim nums() As String = strLine.Split(","c)
    Dim nums() As String = strLine.Split("-")
    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 = TextBox2.Text + intNumber.ToString

    End If
Next
'Update the UI only once, don't force a redraw on each iteration of the loop
'TextBox1.Text = Strings.Left(TextBox1sb.ToString, TextBox1sb.Length - 1)

TextBox1.Text = Mid(TextBox1sb.ToString, 1, Len(TextBox1sb.ToString) - 1)
TextBox2.Text = Mid(TextBox2sb.ToString, 1, Len(TextBox2sb.ToString) - 1)

End Sub 结束子

I hope this can help you bro, ^_^ 希望对您有帮助,^ _ ^

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM