3

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

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()

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

4

3 回答 3

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循环将为我们逐个字符地遍历字符串。
  • 我们检查包含元音的字符串是否包含该字符。如果确实如此,它将返回 true。
  • 如果为真,那么将允许为元音计数 +1。这里的简短表格与您所做的相同。速度上没有区别,只是写起来更简单。注意:一般我们不需要检查=true(或=false)。我们可以直接使用该值:If value ThenIf Not value Then
  • 然后我们通过格式化字符串来呈现结果。{0} 采用我们在逗号后给出的第一个参数({1} 将采用下一个,依此类推)。
  • 通过在例程中本地调暗和初始化,您可以在不重置变量(在本例中为计数器)的情况下重新使用例程。
于 2012-10-13T08:24:30.557 回答
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

希望这可以帮助!

于 2012-10-13T08:07:09.917 回答
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))
于 2012-10-13T08:09:09.390 回答