识别元音

时间:2012-10-13 08:00:08

标签: vb.net

我最近发现很难识别我的作品中的元音,我老师也问我这样做,有没有办法做到这一点?...目前我的代码是。

Dim mystring As String
Dim isitavowel As Boolean
Dim VOWELCOUNT As Integer

Sub Main()

    Console.WriteLine("Enter the text of your choice here")
    mystring = Console.ReadLine
    VOWELCOUNT = 0
    For i = 1 To mystring.Length

        isitavowel = False
        If mystring(i - 1) = "a" Or mystring(i - 1) = "A" Then isitavowel = True
        If mystring(i - 1) = "e" Or mystring(i - 1) = "E" Then isitavowel = True
        If mystring(i - 1) = "i" Or mystring(i - 1) = "I" Then isitavowel = True
        If mystring(i - 1) = "o" Or mystring(i - 1) = "O" Then isitavowel = True
        If mystring(i - 1) = "u" Or mystring(i - 1) = "U" Then isitavowel = True

        If isitavowel = True Then
            VOWELCOUNT = VOWELCOUNT + 1
        End If


    Next
    Console.WriteLine("That had " & VOWELCOUNT & " vowel's in it")
    Console.ReadLine()

问题是有时它有错误。请帮忙!

3 个答案:

答案 0 :(得分:3)

我为您优化了代码。这应该给你一个快速而美好的结果:

Private vowels As String = "aeiou"

Sub Main()

    Console.WriteLine("Enter the text of your choice here")
    Dim mystring As String = Console.ReadLine.ToLower
    Dim VOWELCOUNT As Integer = 0

    For Each c As Char In mystring
        If vowels.Contains(c) Then VOWELCOUNT += 1
    Next

    Console.WriteLine("String contained {0} vowels in it", VOWELCOUNT)
    Console.ReadLine()

End Sub

你声明你对编码很新,所以要仔细阅读代码:

  • 在主要内容中,我们定义了一个包含我们想要识别的所有字符的字符串。
  • 然后在例程中我们直接从ReadLine开始初始化它,因为这将返回一个字符串。
  • 我们小写那个字符串,所以我们可以检查更少的字符
  • 然后for-each循环将遍历我们的字符串,char by char。
  • 我们检查包含元音的字符串是否包含该字符。如果是,它将返回true。
  • 如果为真,则允许为元音计数+1。这里的简短表格和你一样。速度没有区别,只是编写起来比较简单。注意:通常我们不需要检查= true(或= false)。我们可以直接使用该值:If value ThenIf Not value Then
  • 然后我们通过格式化字符串来呈现结果。 {0}采用我们在逗号后给出的第一个参数(a {1}将采用下一个等等。)
  • 通过在例程中本地调暗和初始化,您可以重新使用例程而无需重置变量(在这种情况下为计数器)。

答案 1 :(得分:2)

我已经对您的代码进行了编辑和改进,这是我的解决方案,您不应该遇到任何问题。

  Module Module1
    Dim kMyString As String
    Dim kVowel As Boolean
    Dim kVowelNumber As Integer
    Dim kAnswer As Integer = 0
    Sub Main()
        Console.ForegroundColor = ConsoleColor.DarkGray
        Console.WriteLine("Enter your sentence below:")
        Console.ForegroundColor = ConsoleColor.DarkCyan
        kMyString = Console.ReadLine
        Console.ForegroundColor = ConsoleColor.DarkGray
        For k = 1 To kMyString.Length
            Console.Write(kMyString(k - 1))
            kVowel = False
            If kMyString(k - 1) = "a" Or kMyString = "A" Then kVowel = True
            If kMyString(k - 1) = "e" Or kMyString = "E" Then kVowel = True
            If kMyString(k - 1) = "i" Or kMyString = "I" Then kVowel = True
            If kMyString(k - 1) = "o" Or kMyString = "O" Then kVowel = True
            If kMyString(k - 1) = "u" Or kMyString = "U" Then kVowel = True

            If kVowel Then
                Console.WriteLine(" is a vowel")
                kAnswer = (kAnswer + 1)
            Else
                Console.WriteLine(" isn't a vowel")
            End If
        Next
        Console.ForegroundColor = ConsoleColor.DarkRed
        Console.WriteLine("There are " & kAnswer & " vowels!")
        Console.ReadLine()
    End Sub
End Module

希望这有帮助!

答案 2 :(得分:0)

您可以通过多种方式实现这一目标

但是因为这个作业,你需要阅读最后两篇,现在,我只会帮助你

 For k = 1 To kMyString.Length
    Dim xter = kMyString(k - 1).ToLower  'convert to lowercase
    kVowel = False

    If xter = "a" or xter = "e" or xter = "i" or xter = "o" or xter = "u" Then 
       kVowel = true
       VOWELCOUNT = VOWELCOUNT + 1
    End If
 Next

您的代码现在缩短了。或者有人建议

For k = 1 To kMyString.Length
    Dim xter = kMyString(k - 1).ToLower  'convert to lowercase

    If InStr("aeiou", xter) > 0 Then  //"aeiou".Contains(xter)
       VOWELCOUNT += 1
    End If
Next

LINQ

当您应该显示计算时,这看起来像在考试中使用计算器。可能不适合任务,但稍后会帮助你

Dim VOWELCOUNT As Integer = kMyString.Count(Function(v) "aeiou".Contains(v))