Visual Studio VB 2017中的单词和元音计数

li9yvcax  于 2023-02-09  发布在  其他
关注(0)|答案(3)|浏览(122)

我需要编写一个简单的控制台应用程序,它接受一个输入字符串,然后调用一个子例程来计算字符串中单词的数量和元音的数量。我编写了这个程序,但由于某种原因,它没有输出我在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
llycmphe

llycmphe1#

Words中有以下代码:
Do Until Index = Len(Input)
Index永远不会递增,因此无限循环。
Vowels中也存在相同问题

r1zk6ea1

r1zk6ea12#

下面这些对你有帮助吗?
它不是看一个句子是否包含元音,而是看元音列表是否包含句子中的每个字符。
对于单词计数,它同样会询问包含单个空格的字符串是否包含句子中的每个字符(在本例中,“单词计数”只是空格计数,因此前导或尾随空格,或者单词之间的额外空格,将被计为额外单词,就像您的代码当前所做的那样)。
它还允许我们废除单独的方法调用和手工循环代码,如您所见,这些代码容易产生小错误(我个人建议在这种情况下使用For循环而不是Do Until循环,因为For循环被设计为自动递增索引)。

Sub Main()
    Console.WriteLine("Sentence Analysis")
    Console.WriteLine()
    Console.WriteLine("Enter a sentence then press 'Enter'")
    Console.WriteLine()

    Dim sentence As String
    sentence = Console.ReadLine()

    Dim vowel_count As Integer = sentence.Count(Function(c) "aeiou".Contains(Char.ToLower(c)))
    Dim word_count As Integer = sentence.Count(Function(c) " ".Contains(Char.ToLower(c)))

    Console.WriteLine("Your sentence contains {0} words.", word_count)
    Console.WriteLine("Your sentence contains {0} vowels.", vowel_count)        

    Console.ReadLine()
End Sub
tf7tbtn2

tf7tbtn23#

你和我上同一所大学。这是密码...

Option Compare Text
Imports System
Imports System.Diagnostics.Metrics
Imports System.Reflection
Imports System.Text.RegularExpressions

Module Program
Sub Main(args As String())
    Console.WriteLine("Sentence Analysis")
    Console.WriteLine()
    Console.WriteLine("Enter a sentence, then press 'Enter'")
    Console.WriteLine()
    Dim input As String = Console.ReadLine
    Dim letterbyletter() As Char = input.ToCharArray
    Dim counter As Integer = 1
    Dim vowelcounter As Integer
    Call wordcount(input, letterbyletter, counter)
    Console.WriteLine("Your sentence contains {0} words.", counter)
    Call vowelcount(input, letterbyletter, vowelcounter)
    Console.WriteLine("Your sentence contains {0} vowels.", vowelcounter)
End Sub
Sub wordcount(ByVal input As String, ByRef letterbyletter() As Char, ByRef 
    counter As Integer)
    For i = 0 To Len(input) - 1
        If letterbyletter(i) = " " Then
            counter += 1
        End If
    Next
End Sub
Sub vowelcount(ByVal input As String, ByRef letterbyletter() As Char, ByRef 
    vowelcounter As Integer)
    Dim vowels() As String = {"a", "e", "i", "o", "u"}
    For i = 0 To Len(input) - 1
        For j = 0 To 4
            If letterbyletter(i) = vowels(j) Then
                vowelcounter += 1
            End If
        Next
    Next
    End Sub
    End Module

相关问题