我正在努力让这个元音计数器起作用。我应该能够输入文件(我正在输入输入权限)应该读取文本文档(它没有),然后计算元音。另外,如果我可以,我可以计算单词的数量,但这不是必要的。 错误代码表示变量myline已在当前块中声明。 ??提前谢谢。
以下是代码:
Imports System.IO
Module Module1
Sub Main()
Dim vowel As Integer = 0
Dim Text, myline As String
Dim objStreamReader As StreamReader
objStreamReader = New StreamReader("H:\vowelcounter.txt")
Dim myline = objStreamReader
Text = objStreamReader.ReadLine()
Do While Not Text Is Nothing
Console.WriteLine(text)
For x = 0 To Text.Length
If x = "a" Then
vowel = vowel + 1
End If
If x = "e" Then
vowel = vowel + 1
End If
If x = "i" Then
vowel = vowel + 1
End If
If x = "o" Then
vowel = vowel + 1
End If
If x = "u" Then
vowel = vowel + 1
End If
If x = "A" Then
vowel = vowel + 1
End If
If x = "E" Then
vowel = vowel + 1
End If
If x = "I" Then
vowel = vowel + 1
End If
If x = "O" Then
vowel = vowel + 1
End If
If x = "U" Then
vowel = vowel + 1
End If
Next
Loop
Console.ReadLine()
End Sub
End Module
答案 0 :(得分:2)
错误代码表示变量myline已在声明中声明 目前的阻止。
是的,它是:
Dim Text, myline As String ' <----- HERE
Dim objStreamReader As StreamReader
objStreamReader = New StreamReader("H:\vowelcounter.txt")
Dim myline = objStreamReader ' <----- and HERE
所以重命名第一个字符串变量或StreamReader
(为什么你需要两个一个?)。
你可以更容易地实现元音计数器:
Dim text As String = System.IO.File.ReadAllText("H:\vowelcounter.txt")
Dim vowels = From c In text Where "aeiouAEIOU".Contains(c)
Dim vowelCount As Int32 = vowels.Count()
您可以通过这种方式获得字数:
Dim words = text.Split()
Dim wordCount As Int32 = words.Length
这假定只有空格,制表符或换行符是分隔符。如果你还需要其他角色:
Dim wordDelimiter As Char() = {" "C, ControlChars.Tab, ","C, "."C, "!"C, "?"C, _
";"C, ":"C, "/"C, "\"C, "["C, "]"C, _
"("C, ")"C, "<"C, ">"C, "@"C, """"C, _
"'"C}
Dim words = text.Split(wordDelimiter, StringSplitOptions.None)