我很努力使這個元音計數器正常工作。我應該可以輸入文件(我是否正在輸入權限)應該讀取文本文件(它沒有),然後計算元音。另外,如果我能夠讓它計算單詞的數量,但這不是必需的。 錯誤代碼表示變量myline已在當前塊中聲明。 ??提前致謝。在VB中破壞的元音計數器
下面是代碼:
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
'x'不會等於一個字符,因爲它是一個介於0和'text.length'之間的整數。 –
您也正在使用實現「iDisposable」的對象,並且在代碼結束時尚未關閉它。你需要使用'Using'語句。 –
只要在執行循環迭代時Text不爲Nothing,While循環就會無限次地運行,因爲它在整個循環中都沒有改變。 –