我在下面有以下子。它的作用是从用户输入的文本框中获取输入,用特定的数值替换每个字母,显示每个单词的总数,然后显示整个输入的总数。例如,如果用户键入 aa bbb e,则 txtbox5 中的输出为:
aa = 40
bbb = 90
e = 40
Total 170
如果用户输入一个长句子,那效果很好。所以我想以相同的方式分别计算每个句子,句子的分隔符可以是句号或逗号。如果用户键入 aa bbb。嗯嗯。输出应该
aa = 40
ccc = 90
Total for the 1st sentence = 120
ff = 100
ee = 80
Total for the 2nd sentence = 180
and so forth
Private Sub Calculate(ByVal input As String)
Dim total As Integer = 0
Dim wordTotal As Integer
Dim dicLetters As New Dictionary(Of Char, Integer)
dicLetters.Add("A", 20)
dicLetters.Add("B", 30)
dicLetters.Add("E", 40)
dicLetters.Add("F", 50)
Dim charValue As Integer
For Each word As String In input.Split(New Char() {" "})
wordTotal = 0
For Each character As Char In word
wordTotal += If(dicLetters.TryGetValue(character, charValue) = _
True, dicLetters(character), 0)
Next
total += wordTotal
txtBox5.Text += word.PadRight(12) + " = " + _
wordTotal.ToString().PadLeft(5) + vbNewLine
Next
txtBox5.Text += "Total:".PadRight(12) + " = " + _
total.ToString().PadLeft(5)
End Sub