2016-01-13 57 views
-2

嗨我需要將WordCount和CountVowel過程更改爲函數,並創建一個函數來計算字符串中輔音的數量。CountWord和CountVowel在VB.NET中進入函數

我已經完成了這兩個程序,但我不知道如何做最後一部分。我對編程相當陌生。下面

我當前的代碼給出:

Sub Main() 
    Dim Sentence As String 
    Console.WriteLine("Sentence Analysis" + vbNewLine + "") 
    Console.WriteLine("Enter a sentence, then press 'Enter'" + vbNewLine + "") 
    Sentence = Console.ReadLine() 
    Console.WriteLine("") 
    Call WordCount(Sentence) 
    Call VowelCount(Sentence) 
    Console.ReadLine() 
End Sub 
Sub WordCount(ByVal UserInput As String) 
    Dim Space As String() = UserInput.Split(" ") 
    Console.WriteLine("There are {0} words", Space.Length) 
End Sub 
Sub VowelCount(ByVal UserInput As String) 
    Dim i As Integer 
    Dim VowelNumber As Integer 
    Dim Vowels As String = "aeiou" 
    For i = 1 To Len(UserInput) 
     If InStr(Vowels, Mid(UserInput, i, 1)) Then 
      VowelNumber = VowelNumber + 1 
     End If 
    Next 
    Console.WriteLine("There are {0} vowels", VowelNumber) 
End Sub 

感謝您的時間

回答

0

你在這裏。

Function WordCount(ByVal UserInput As String) As Integer 
    Dim Space As String() = UserInput.Split(" ") 
    Return Space.Length 
End Function 

Function VowelCount(ByVal UserInput As String) As Integer 
    Dim i As Integer 
    Dim VowelNumber As Integer 
    Dim Vowels As String = "aeiou" 
    For i = 1 To Len(UserInput) 
     If InStr(Vowels, Mid(UserInput, i, 1)) Then 
      VowelNumber = VowelNumber + 1 
     End If 
    Next 
    Return VowelNumber 
End Function 

Function ConsonantCount(ByVal UserInput As String) As Integer 
    Dim i As Integer 
    Dim ConsonantNumber As Integer 
    Dim Consonants As String = "bcdfghjklmnpqrstvwxyz" 
    For i = 1 To Len(UserInput) 
     If InStr(Consonants, Mid(UserInput, i, 1)) Then 
      ConsonantNumber = ConsonantNumber + 1 
     End If 
    Next 
    Return ConsonantNumber 
End Function 
2

你可以使用Regex類。它旨在使用模式來搜索子串,而且它的速度也相當快。

Sub VowelCount(ByVal UserInput As String) 
    Console.WriteLine("There are {0} vowels", System.Text.RegularExpressions.Regex.Matches(UserInput, "[aeiou]", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Count.ToString()) 
End Sub 

[aeiou]是執行搜索時使用的模式。它與您在括號內寫入的任何字符相匹配。

例子:

http://ideone.com/LEYC30

瞭解更多關於正則表達式:

MSDN - .NET Framework Regular Expressions

MSDN - Regular Expression Language - Quick Reference

+0

當然,但對於剛剛嘗試學習編程基礎知識的人來說,這似乎是一個奇怪的建議。 –

1

VB不再是語言I U經常使用,但我認爲即使沒有對此進行測試,我也不會誤導你。

Sub Main() 
    Dim Sentence As String 
    Console.WriteLine("Sentence Analysis" + vbNewLine + "") 
    Console.WriteLine("Enter a sentence, then press 'Enter'" + vbNewLine + "") 
    Sentence = Console.ReadLine() 
    Console.WriteLine("") 

    'usually it's better just let the function calculate a value and do output elsewhere 
    'so I've commented your original calls so you can see where they used to be 
    'Call WordCount(Sentence) 
    Console.WriteLine("There are {0} words", WordCount(Sentence)) 
    'Call VowelCount(Sentence) 
    Console.WriteLine("There are {0} vowels", VowelCount(Sentence)) 
    Console.ReadLine() 
End Sub 

Function WordCount(ByVal UserInput As String) As Integer 
    Dim Space As String() = UserInput.Split(" ") 
    WordCount = Space.Length 
    'or just shorten it to one line... 
    'Return UserInput.Split(" ").Length 
End Function 

Function VowelCount(ByVal UserInput As String) As Integer 
    Dim i As Integer 
    Dim VowelNumber As Integer 
    Dim Vowels As String = "aeiou" 
    For i = 1 To Len(UserInput) 
     If InStr(Vowels, Mid(UserInput, i, 1)) Then 
      VowelNumber = VowelNumber + 1 
     End If 
    Next 
    VowelCount = VowelNumber 
End Function 

一個subfunction之間最明顯的變化就是把那個包裹起來程序的關鍵字。對於這個對話,讓我們只是說這是一個很好的詞彙,因爲它們非常相似,許多語言並沒有真正吸引這麼大的區別。

對於Visual Basic的目的,一個函數需要返回一些東西,這是我在兩個函數聲明的末尾添加的​​所表示的(不記得這是否是正確的VB術語。)同樣在VB中,您返回通過分配給函數的名稱給調用者一個值(也見下面的編輯。)因此,我用適當的賦值替換了那些行爲WriteLine。最後,我將這些WriteLine報表上移至Main。參數需要改變,以使用函數返回值而不是它們最初引用的變量。

希望我沒有爲你做功課!

編輯:Visual Basic在2000年初移動到.Net期間經歷了很多語言的變化。我忘記了(或者甚至沒有意識到)返回值的新首選現在更符合像C#這樣的語言。因此,您可以僅使用Return,而不是將值分配給WordCountVowelCount。二者之間的一個區別是,Return將導致子/功能在該點退出,即使之後有其他代碼。例如,這可能在if...end if內有用。我希望這可以幫助你學到一些東西,而不僅僅是困惑。

編輯#2:現在我看到接受的答案,並重新閱讀這個問題,似乎有一小部分關於計算忽略的輔音。在這一點上,我認爲這確實是一個課堂練習,並且預期的答案甚至可能是通過使用其他函數來獲得輔音數。

3

我會使用以下三個函數。請注意,WordCount使用RemoveEmptyEntries可避免在單詞之間有多個空格時對空字進行計數。

另外兩個函數將大寫元音計爲元音,而不是小寫。他們利用字符串可以被視爲Char數組的事實,並使用Count方法來計算這些Chars中有多少符合某些標準。

請注意,「AEIOU」作爲元音的指定在所有語言中可能不正確,甚至英語「Y」有時也被認爲是元音。您可能還需要考慮重音字母(如「É」)的可能性。

Function WordCount(UserInput As String) As Integer 
    Return UserInput.Split({" "c}, StringSplitOptions.RemoveEmptyEntries).Length 
End Function 

Function VowelCount(UserInput As String) As Integer 
    Return UserInput.Count(Function(c) "aeiouAEIOU".Contains(c)) 
End Function 

Function ConsonantCount(UserInput As String) As Integer 
    Return UserInput.Count(Function(c) Char.IsLetter(c) And Not "aeiouAEIOU".Contains(c)) 
End Function 
+0

我原本以爲輔音功能與您所寫的輔音功能相似,但我意識到它會返回所有內容,包括標點符號和數字。可悲的是我很尷尬承認我不知道.IsLetter。看來你和我是唯一已經正確地閱讀了這個問題的人。 –

2

要打開每個Sub例程成Function,你需要做三件事情。首先,您需要將SubEnd Sub關鍵字分別更改爲FunctionEnd Function。所以:

Sub MyMethod(input As String) 
    ' ... 
End Sub 

變爲:

Function MyMethod(input As String) 
    ' ... 
End Function 

接下來,因爲它是一個功能,它需要返回一個值,所以你Function聲明只需要指定返回值的類型。因此,上述示例將變爲:

Function MyMethod(input As String) As Integer 
    ' ... 
End Function 

最後,函數中的代碼必須實際指定返回值是什麼。在VB.NET,這是通過使用Return關鍵字來完成,像這樣:

Function MyMethod(input As String) As Integer 
    Dim result As Integer 
    ' ... 
    Return result 
End Function 

所以,適用於您的例子:

Sub WordCount(ByVal UserInput As String) 
    Dim Space As String() = UserInput.Split(" ") 
    Console.WriteLine("There are {0} words", Space.Length) 
End Sub 

將成爲:

Function WordCount(userInput As String) As Integer 
    Dim Space As String() = UserInput.Split(" ") 
    Return Space.Length 
End Sub 

請注意,ByVal是默認值,因此您不需要指定它,並且通過.NET中的標準約定,參數變量應該是camelCase而不是PascalCase。然後,當你調用該方法,您可以使用函數的這樣的返回值:

Dim count As Integer = WordCount(Sentence) 
Console.WriteLine("There are {0} words", count) 

至於計數輔音去,那將是非常相似,你VowelCount方法,除非你想給它要查找的輔音列表而不是元音。