我需要编写一个简单的控制台应用程序,它接受一个输入字符串,然后调用一个子例程来计算字符串中单词的数量和元音的数量。我编写了这个程序,但由于某种原因,它没有输出我在Console.Writeline
代码中输入的文本。有帮助吗?
Module Module1
Sub Main()
Dim Sentence As String
Console.WriteLine("Sentence Analysis")
Console.WriteLine()
Console.WriteLine("Enter a sentence then press 'Enter'")
Console.WriteLine()
Sentence = Console.ReadLine()
Sentence = Sentence.ToUpper
Call Words(Sentence)
Call Vowels(Sentence)
Console.ReadLine()
End Sub
Sub Words(ByVal Input As String)
Dim Count As Integer
Dim Index As Short
Dim Character As Char
Do Until Index = Len(Input)
Character = Input.Substring(Index)
If Character = " " Then
Count = Count + 1
End If
Loop
Console.WriteLine("Your sentence contains {0} words.", (Count))
End Sub
Sub Vowels(ByVal Input As String)
Dim Count As Integer
Dim Vowels() As String = {"A", "E", "I", "O", "U"}
Dim Index As Short
Dim Character As Char
Do Until Index = Len(Input)
Character = Input.Substring(Index)
If Character = Vowels(Index) Then
Count = +1
End If
Loop
Console.WriteLine("Your sentence contains {0} words.", (Count))
End Sub
End Module
3条答案
按热度按时间llycmphe1#
在
Words
中有以下代码:Do Until Index = Len(Input)
Index永远不会递增,因此无限循环。
Vowels
中也存在相同问题r1zk6ea12#
下面这些对你有帮助吗?
它不是看一个句子是否包含元音,而是看元音列表是否包含句子中的每个字符。
对于单词计数,它同样会询问包含单个空格的字符串是否包含句子中的每个字符(在本例中,“单词计数”只是空格计数,因此前导或尾随空格,或者单词之间的额外空格,将被计为额外单词,就像您的代码当前所做的那样)。
它还允许我们废除单独的方法调用和手工循环代码,如您所见,这些代码容易产生小错误(我个人建议在这种情况下使用
For
循环而不是Do Until
循环,因为For
循环被设计为自动递增索引)。tf7tbtn23#
你和我上同一所大学。这是密码...