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

时间:2016-01-27 05:22:02

标签: vb.net string char string-function

我试图找到数字的位数,最低位数,最高位数以及字符串中所有数字的平均值。

到目前为止,这是我能够提出的

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

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

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

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

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

3 个答案:

答案 0 :(得分:1)

第一个问题是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

你会得到这个:

enter image description here

答案 1 :(得分:0)

Asc(c)返回数字字符的ASCII值,例如'0'的ASCII值为48。

假设您的目标是忽略非数字并平均其余数字,您几乎可以得到正确的平均值,您可以调整0 = 48,1 = 49等。您需要执行此步骤

averageInt = averageInt / i
循环完成后

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

答案 2 :(得分:0)

使用regexList

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

中整数的模式