查找字符串中所有数字的平均值

Find average of all digits in string

我正在尝试计算数字的个数、最低位、最高位以及字符串中所有数字的平均值。

这是我到目前为止能想到的

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim i As Integer = 0
        Dim minimumInt As Integer
        Dim maximumInt As Integer
        Dim averageInt As Single
        For Each c As Char In TextBox1.Text
            If Char.IsDigit(c) Then
                i = i + 1
                If (Asc(c) < minimumInt) Then
                    minimumInt = Asc(c)
                ElseIf (Asc(c) > maximumInt) Then
                    maximumInt = Asc(c)
                End If
                averageInt = averageInt + Asc(c)
                averageInt = averageInt / i
            End If
        Next
        MessageBox.Show("Total Digits: " & i & ControlChars.CrLf & "Average Number: " & averageInt & ControlChars.CrLf & "Minimum Number: " & minimumInt & ControlChars.CrLf & "Maximum Number: " & maximumInt)
    End Sub
End Class

我认为问题涉及将字符转换为整数。

例如,当TextBox1包含"X13210AS"时,我得到这个 http://i.imgur.com/5aaOWC0.png

当我应该得到平均 1.4 和最高数字 3 时。

关于如何在仍然使用我的大部分代码的情况下解决此问题的任何想法?

Asc(c) returns 数字字符的 ASCII 值,例如“0”的 ASCII 值为 48。

假设您的目标是忽略 non-digits 并对其余部分进行平均,您几乎可以得到正确的平均值,调整为 0 = 48、1 = 49 等。您需要执行此步骤

averageInt = averageInt / i

循环完成后。

您的最小值为 0,因为 minimumInt 从 0 开始。从 minimumInt = Asc('9') 或类似的高值开始。

第一个问题是 ASCII 转换。 “0”个字符的值为 48。

第二个问题是你的平均,应该在你得到所有数字而不是每个循环之后完成。 小心除以 0!

要做到正确,您必须这样做:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim i As Integer = 0
        Dim minimumInt As Integer
        Dim maximumInt As Integer
        Dim averageInt As Single
        Dim intVal As Integer 'only use this
        For Each c As Char In TextBox1.Text
            If Char.IsDigit(c) Then
                i = i + 1
                intVal = Asc(c) - 48 'count this once, note -48 here is to convert ASCII rep to integer value
                If (intVal < minimumInt) Then
                    minimumInt = intVal
                ElseIf (intVal > maximumInt) Then
                    maximumInt = intVal
                End If
                averageInt += intVal
            End If
        Next
        If i <> 0 Then 'beware of 0
            averageInt = averageInt / i `put this outside
        End If
        MessageBox.Show("Total Digits: " & i & ControlChars.CrLf & "Average Number: " & averageInt & ControlChars.CrLf & "Minimum Number: " & minimumInt & ControlChars.CrLf & "Maximum Number: " & maximumInt)
    End Sub
End Class

你会得到这个:

使用regex and List

Imports System.Text.RegularExpressions

你可以通过简单的方式做到这一点

TextBox1.Text = "X13210AS"
Dim lstnums As List(Of Integer) = Regex.Replace(TextBox1.Text, "[^\d]", ""). _
                                  Select(Function(x) Val(x)).ToList
Dim maxnum, minnum As Integer
Dim avg As Double

maxnum = lstnums.Max()
minnum = lstnums.Min()
avg = lstnums.Average
MessageBox.Show("Total Digits: " & Val(lstnums.Count) & ControlChars.CrLf & "Average Number: " & avg & ControlChars.CrLf & "Minimum Number: " & minnum & ControlChars.CrLf & "Maximum Number: " & maxnum)

[^\d] 是匹配 Textbox1

中整数的模式