我最近发现很难识别我的作品中的元音,我老师也问我这样做,有没有办法做到这一点?...目前我的代码是。
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()
问题是有时它有错误。请帮忙!
答案 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
你声明你对编码很新,所以要仔细阅读代码:
If value Then
或If Not value Then
答案 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
当您应该显示计算时,这看起来像在考试中使用计算器。可能不适合任务,但稍后会帮助你
Dim VOWELCOUNT As Integer = kMyString.Count(Function(v) "aeiou".Contains(v))