多行中的偶数/奇数

时间:2019-04-11 11:15:05

标签: vb.net

如何显示奇数/偶数?我如何计算每行多少?它不能正常工作,只显示一个值,而不是全部。

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 i As Integer = 1 To 100
        For Each strNum As String In TxtBoxIntDraws.Lines(i).Split(",")
        Next
        If Integer.TryParse(strNum.Trim, number) Then
            numbers.Add(number)
        End If
    Next
    numbers.Sort()
    If (number / 2) = Int(number / 2) Then
        'Number is even
        TextBox1.Text &= Environment.Newlinw & TextBox1.Text & "," & +number
    Else
        TextBox2.Text &= Environment.Newlinw & TextBox2.Text & "," & +number
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

行中的评论。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim number As Integer
    Dim numbers As New List(Of Integer)
    For Each strLine As String In TextBox1.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)
        Else
            TextBox2sb.AppendLine(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