Vb.net 在visualbasic中使用函数

Vb.net 在visualbasic中使用函数,vb.net,Vb.net,我正在使用的程序有两个不同的函数,一个用于计算文本文件中的音节数,另一个用于根据公式计算文本文件的可读性 206.835-85.6*(Number of Syllables/Number of Words)-1.015*(Number of Words/Number of Sentences) 以下是我遇到的问题: 我应该在多行文本框中显示文本文件的内容 我应该在文本框下方的标签中显示从函数indexCalculation得到的答案 调用函数使程序计算要显示在标签中的答案时遇到问题 这是我

我正在使用的程序有两个不同的函数,一个用于计算文本文件中的音节数,另一个用于根据公式计算文本文件的可读性

206.835-85.6*(Number of Syllables/Number of Words)-1.015*(Number of Words/Number of   Sentences)
以下是我遇到的问题:

  • 我应该在多行文本框中显示文本文件的内容
  • 我应该在文本框下方的标签中显示从函数
    indexCalculation
    得到的答案
  • 调用函数使程序计算要显示在标签中的答案时遇到问题 这是我到目前为止的代码

    Option Strict On
    
    Imports System.IO
    
    Public Class Form1
    
        Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
            Me.Close()
        End Sub
    
        Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
            Dim open As New OpenFileDialog
    
            open.Filter = "text files |project7.txt|All file |*.*"
            open.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
    
            If open.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim selectedFileName As String = System.IO.Path.GetFileName(open.FileName)
                If selectedFileName.ToLower = "project7.txt" Then
                        Dim text As String = File.ReadAllText("Project7.txt")
                        Dim words = text.Split(" "c)
                        Dim wordCount As Integer = words.Length
                        Dim separators As Char() = {"."c, "!"c, "?"c, ":"c}
                        Dim sentences = text.Split(separators, StringSplitOptions.RemoveEmptyEntries)
                    Dim sentenceCount As Integer = sentences.Length
                    Dim vowelCount As Integer = 0
                    For Each word As String In words
                    vowelCount += CountSyllables(word)
                    Next
                    vowelCount = CountSyllables(text)
                    Label1.Show(indexCalculation(wordCount, sentenceCount, vowelCount))
                Else
                    MessageBox.Show("You cannot use that file!")
                End If
            End If
    
        End Sub
    
        Function CountSyllables(word As String) As Integer
            word = word.ToLower()
            Dim dipthongs = {"oo", "ou", "ie", "oi", "ea", "ee", _
                             "eu", "ai", "ua", "ue", "au", "io"}
            For Each dipthong In dipthongs
                word = word.Replace(dipthong, dipthong(0))
            Next
            Dim vowels = "aeiou"
            Dim vowelCount = 0
            For Each c In word
                If vowels.IndexOf(c) >= 0 Then vowelCount += 1
            Next
            If vowelCount = 0 Then
                vowelCount = 1
            End If
            Return vowelCount
        End Function
    
        Function indexCalculation(ByRef wordCount As Integer, ByRef sentenceCount As Integer, ByRef vowelCount As Integer) As Integer
            Dim answer As Integer = CInt(206.835 - 85.6 * (vowelCount / wordCount) - 1.015 * (wordCount / sentenceCount))
            Return answer
        End Function
    End Class
    

    如有任何建议,将不胜感激

    以下是我的建议:

  • 更新您的indexCalculation函数以接受整数,而不是字符串。这样你就不必把它们转换成数字
  • 删除所有未使用的额外变量。这会把事情弄清楚一点
  • 删除您的streamreader。您似乎正在通过File.ReadAllText读取文本
  • Label1.Show(答案)应更改为Label1.Show(indexCalculation(wordCount,sentenceCount,vouelcount))--除非Label1不是常规标签,否则使用Label1.Text=indexCalculation(wordCount,sentenceCount,vouelcount))
  • 然后,对于元音计数,您需要执行以下操作:

    Dim vowelCount as Integer = 0
    For Each word as String in words
     vowelCount += CountSyllables(word)
    Next
    

    另外,将逻辑添加到CountSypelles函数中,使其在0时为1。如果您不想在元音计数中包含最后一个字符,请使用for循环,而不是for-each循环,并缩短一个字符。

    谢谢您,这非常有用,现在我唯一的问题是元音计数,它表示尚未声明,有什么想法吗?另外,关于如何让文件内容显示在多行文本框中,您有什么建议吗?好的,您需要声明变量并将其设置为这样
    Dim volwelCount as Integer=countsylleles(text)
    。虽然我可能会将函数名改为countvoters,因为这是您要返回的。还有,这应该做什么<代码>单词=单词。替换(双管接头,双管接头(0))?现在,它不起作用,因为你试图引用双簧管中的第一个项目,但是这不是一个数组。这个程序的目标是计算单词,句子,和文本文件中的音节,然后将这些值应用于公式,并在多行文本框中显示文件的文本以及标签中公式的答案。这是别人给我的一个建议,让我数数音节,因为我不知道该怎么做。那么我猜您正在尝试执行以下操作:
    word=word.Replace(dipthong,“a”)
    。这实际上删除了任何多元音对,并替换为单个元音对。我会在索引计算中重新考虑您的答案变量。您将其全部转换为整数,但您有混合类型。您已经硬编码了类型为decimal之类的值,将其中任何一个转换为整数都会使您失去所有精度。只是一个想法。。。