我的元音计数器在VB中不算数

时间:2015-11-04 20:37:49

标签: vb.net

我需要在vb中创建一个元音然后单词计数器,它将无法正常工作。我刚刚尝试了元音位,但它不起作用。有什么想法吗?

    Dim analsyedtext As String
    Dim lettercount As Integer
    Dim i As Integer
    lettercount = 0
    Console.WriteLine("Please enter the text to be analysed.")
    analsyedtext = Console.ReadLine()
    If analsyedtext = "a" Or "e" Or "i" Or "o" Or "u" Then
        lettercount = lettercount + 1
    End If
    Console.Writeline("The number of vowels is,{0}", lettercount)
End Sub

3 个答案:

答案 0 :(得分:3)

' My preferred function
Private Function VowelCount(str As String) As Integer
    Dim vowels() As Char = "aeiou".ToCharArray()
    Return (From letter In str.ToLower().ToCharArray()
            Where vowels.Contains(letter)
            Select letter).Count()
End Function
' This tests all of the functions and Asserts they're identical output
Sub Main()
    For Each itm In {"this", "is", "some sort of", "a test that we're doing"}
        Dim vowelNum = VowelCount(itm)
        Console.WriteLine("{0}: {1}", itm, vowelNum)
        Debug.Assert(vowelNum = VowelCount2(itm) AndAlso
                     vowelNum = VowelCount3(itm) AndAlso
                     vowelNum = VowelCount4(itm) AndAlso
                     vowelNum = VowelCount5(itm))
    Next
    Console.ReadLine()
End Sub
' Identical to above, different syntax
Private Function VowelCount2(str As String) As Integer
    Dim vowels() As Char = "aeiou".ToCharArray()
    Return str.ToLower().ToCharArray().Where(Function(ltr As Char) vowels.Contains(ltr)).Count()
End Function
' Uses another function IsVowel that does the same thing as vowels.Contains()
Private Function VowelCount3(str As String) As Integer
    Dim vowelsInStr As New List(Of Char)
    For Each letter As Char In str.ToLower().ToCharArray()
        If IsVowel(letter) Then
            vowelsInStr.Add(letter)
        End If
    Next
    Return vowelsInStr.Count
End Function
' Different since this doesn't first put vowels into an IEnumerable and then count the vowels, it only does the count
Private Function VowelCount4(str As String) As Integer
    Dim vowels() As Char = "aeiou".ToCharArray()
    Dim count As Integer
    For Each letter In str.ToLower().ToCharArray()
        If IsVowel2(letter) Then
            count += 1
        End If
    Next
    Return count
End Function
' Same as above but uses a For loop instead of For Each
Private Function VowelCount5(str As String) As Integer
    Dim vowels() As Char = "aeiou".ToCharArray()
    Dim count As Integer
    Dim letters() As Char = str.ToLower().ToCharArray()
    For i = 0 To letters.Length - 1
        If IsVowel2(letters(i)) Then
            count += 1
        End If
    Next
    Return count
End Function

Private Function IsVowel(ltr As Char) As Boolean
    Dim vowels() As Char = "aeiou".ToCharArray()
    Return vowels.Contains(ltr)
End Function

Private Function IsVowel2(ltr As Char) As Boolean
    Dim vowels() As Char = "aeiou".ToCharArray()
    For Each vowel As Char In vowels
        If vowel = ltr Then
            Return True
        End If
    Next
    Return False
End Function

答案 1 :(得分:0)

编辑:刚认识到还有更多的工作需要你去做每封信。但假设你达到analsyedtext是一个字母的地步:

Select Case analsyedtext
    Case "a", "e", "i", "o", "u"
        lettercount += 1
    Case Else
        ' Do nothing
End Select

答案 2 :(得分:0)

您需要做的是一次检查一个字符的输入字符串。

您可以使用SubString这样的功能(而不是If..Then..End If代码段)一次提取一个字符:

For i = 0 To analsyedtext.Length - 1
    Dim c = analsyedtext.Substring(i, 1)
    If c = "a" OrElse c = "e" OrElse c = "i" OrElse c = "o" OrElse c = "u" Then
        lettercount = lettercount + 1
    End If
Next

注意它是如何从零开始并转到analsyedtext.Length - 1 - 这是因为第一个字符的索引为零。