我有以下函數,其輸入來自txtbox1
的輸入並輸出結果txtbox2
。要點是用一個特定的數值代替每個字母,計算每個字的值,然後顯示所有字的總數。現在,這個函數總是計算到13.如果我輸入aaa bbb cc
例如,結果應該是。我如何修改函數來做到這一點?計算每個字母和總數vb.net
aaa = 3
bbb = 15
cc = 14
Total = 32
Private Sub CountLetters(Input As String)
Dim total As Integer = 0
Dim dicLetters As New Dictionary(Of Char, Integer)
dicLetters.Add("a", 1)
dicLetters.Add("b", 5)
dicLetters.Add("c", 7)
For Each word As String In Input.Split
Dim wordtotal As Integer = 0
For Each cc As KeyValuePair(Of Char, Integer) In dicLetters
wordtotal += cc.Value
Next
total += wordtotal
'Display word totals here
txtBox2.Text += word.PadRight(12) + "=" + _
wordtotal.ToString.PadLeft(5) + vbNewLine
Next
'Display total here
txtBox2.Text += "Total".PadRight(12) + "=" + total.ToString.PadLeft(5)
End Sub
謝謝@tim! – logixologist
@logixologist - 不客氣。我希望我沒有偷你的雷霆...... – Tim
我更喜歡你的解決方案:)。 – logixologist