问题
I'm trying to find the count of the digits, the lowest digit, the highest digit, and the average of all the digits in a string.
This is what I was able to come up with so far
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
I believe the issue is involving converting Chars to Integers.
For example, when TextBox1 contains "X13210AS", I get this http://i.imgur.com/5aaOWC0.png
When I should be getting an average of 1.4 and highest digit 3.
Any ideas on how to fix this while still using the majority of my code?
回答1:
The first issue is with the ASCII conversion. "0" char is having value of 48.
The second issue is with your averaging, which should be done after you get all numbers instead of each loop. And beware of division by 0!
To get it right, you have to do this:
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
And you will get this:
回答2:
Asc(c)
returns the ASCII value of the digit character, so e.g. '0' has an ASCII value of 48.
Assuming your goal is to ignore non-digits and average the rest, you will almost get you the correct average of you adjust for 0 = 48, 1 = 49, etc. You need to do this step
averageInt = averageInt / i
after the loop is done.
Your minimum value is 0 because minimumInt starts as 0. Start with minimumInt = Asc('9') or a similar high value.
回答3:
Using regex and List
Imports System.Text.RegularExpressions
you can do it in a simple way
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]
is the pattern to match integer in Textbox1
来源:https://stackoverflow.com/questions/35029456/find-average-of-all-digits-in-string