2

我在用户输入一些文本的表单上有一个文本框。每个字母被分配一个不同的值,如 a = 1、b = 2、c = 3 等等。例如,如果用户键入“aa bb ccc”,则标签上的输出应如下所示:

aa = 2
bb = 4
dd = 6
Total value is (12)

我能够通过循环遍历文本框字符串来获得总值,但是如何显示每个单词的总值。这是我到目前为止所拥有的:

For letter_counter = 1 To word_length
    letter = Mid(txtBox1.Text, letter_counter, 1)

    If letter.ToUpper = "A" Then
        letter_value = 1
    End If
    If letter.ToUpper = "B" Then
        letter_value = 2
    End If
    If letter.ToUpper = "C" Then
        letter_value = 3
    End If
    If letter.ToUpper = "D" Then
        letter_value = 4
    End If
    If letter.ToUpper = "E" Then
        letter_value = 5
    End If
    If letter.ToUpper = " " Then    
        letter_value = 0
    End If

    totalletter = totalletter + letter_value

    Label1.Text = Label1.Text & letter_value & " "
    txtBox2.Text = txtBox2.Text & letter_value & " "
Next letter_counter
4

5 回答 5

2

这应该给你一个想法:

Dim listOfWordValues As New List(Of Integer)
For letter_counter = 1 To word_length
    letter = Mid(txtBox1.Text, letter_counter, 1)

    If letter = " " Then 
        totalletter= totalletter + letter_value   
        listOfWordValues.Add(letter_value)
        letter_value  = 0
     Else
        letter_value += Asc(letter.ToUpper) - 64
     End If
Next letter_counter

totalletter = totalletter + letter_value
If Not txtBox1.Text.EndsWith(" ") Then listOfWordValues.Add(letter_value)
txtBox2.Text = txtBox2.Text & string.Join(", ", listOFWordValues);
于 2013-06-30T07:51:56.360 回答
2

这个简单的小程序应该可以解决问题:

Private Sub CountLetters(Input As String)
    Label1.Text = ""
    Dim total As Integer = 0
    Dim dicLetters As New Dictionary(Of Char, Integer)
    dicLetters.Add("a"c, 1)
    dicLetters.Add("b"c, 5)
    dicLetters.Add("c"c, 7)
    For Each word As String In Input.Split
        Dim wordtotal As Integer = 0
        For Each c As Char In word
            wordtotal += dicLetters(Char.ToLower(c))
        Next
        total += wordtotal
        'Display word totals here
        Label1.Text += word.PadRight(12) + "=" + wordtotal.ToString.PadLeft(5) + vbNewLine
    Next
    'Display total here
    Label1.Text += "Total".PadRight(12) + "=" + total.ToString.PadLeft(5)
End Sub
于 2013-06-30T10:34:04.160 回答
0

你可以尝试这样的事情。假设 txtBox1 是用户输入的字符串,“”(空格)是单词分隔符:

Dim words As String() = txtBox1.Text.Split(New Char() {" "}, StringSplitOptions.RemoveEmptyEntries)

Dim totalValue As Integer = 0
Dim wordValue As Integer = 0

For Each word As String In words

    wordValue = 0

    For letter_counter = 1 To word.Length

        Dim letter As String = Mid(txtBox1.Text, letter_counter, 1)
        Select letter.ToUpper()
          Case "A":
              wordValue = wordValue + 1
          Case "B":
              wordValue = wordValue + 2
          ' And so on
        End Select
    Next

    totalValue = toalValue + wordValue
Next

上面的代码首先从用户那里获取输入的文本并将其拆分为“”(空格)。

接下来它设置两个变量 - 一个用于总值,一个用于单个单词值,并将它们初始化为 0。

外部循环从对用户输入的文本执行的拆分中遍历数组中的每个单词。在此循环开始时,它将 wordValue 计数器重置为 0。

内部循环遍历当前单词,并通过Select语句汇总字母的值。

一旦内部循环退出,该单词的总值将被添加到正在运行的 totalValue 中,并评估下一个单词。

在这两个循环结束时,您将计算每个单词的值以及所有世界的总数。

我的示例中唯一未包含的内容是更新您的标签。

于 2013-06-30T07:54:57.087 回答
0

试试这个 ..

    Dim s As String = TextBox1.Text
    Dim c As String = "ABCDE"
    Dim s0 As String
    Dim totalletter As Integer

    For x As Integer = 0 To s.Length - 1
        s0 = s.Substring(x, 1).ToUpper
        If c.Contains(s0) Then
            totalletter += c.IndexOf(s0) + 1
        End If
    Next
    MsgBox(totalletter)
于 2013-06-30T09:04:16.397 回答
0

我会使用将每个字母映射到数字的字典来解决这个问题。

Private Shared ReadOnly LetterValues As Dictionary(Of Char, Integer) = GetValues()

Private Shared Function GetValues() As IEnumerable(Of KeyValuePair(Of Char, Integer))
    Dim values As New Dictionary(Of Char, Integer)
    Dim value As Integer = 0
    For Each letter As Char In "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        value += 1
        values.Add(letter, value)
    Next
    Return values
End Function

Public Function CalculateValue(input As String) As Integer
    Dim sum As Integer = 0
    For Each letter As Char In input.ToUpperInvariant()
        If LetterValues.ContainsKey(letter) Then
            sum += LetterValues.Item(letter)
        End If
    Next
    Return sum
End Function

使用示例:

    Dim sum As Integer = 0
    For Each segment As String In "aa bb ccc".Split()
        Dim value = CalculateValue(segment)
        Console.WriteLine("{0} = {1}", segment, value)
        sum += value
    Next
    Console.WriteLine("Total value is {0}", sum)

    ' Output
    ' aa = 2
    ' bb = 4
    ' ccc = 9
    ' Total value is 15
于 2013-06-30T11:20:56.433 回答