我需要编写一个简单的控制台应用程序,它接受一个输入字符串,然后调用一个子例程来计算字符串中的单词数量和元音量。我已经写了这个,由于某种原因它没有输出我在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
答案 0 :(得分:3)
在Words
中,您有以下代码:
Do Until Index = Len(Input)
索引永远不会增加,因此无限循环。
Vowels
中的同样问题
答案 1 :(得分:0)
以下是对您有任何帮助吗?
不是查看句子是否包含元音,而是查看元音列表是否包含句子中的每个字符。
对于单词count,它同样会询问包含单个空格的字符串是否包含句子中的每个字符("字数"在这种情况下只是空格的数量,因此前导或尾随空格,或者单词之间的额外空格,将被视为额外的单词,正如您的代码目前所做的那样。)
它还允许我们废除单独的方法调用,以及手动循环代码,正如您所看到的,它容易出现小错误(我个人建议使用For
循环而不是{ {1}}在这种情况下循环,因为Do Until
循环被设计为自动递增索引。)
For