在Visual Basic中使用函数

时间:2014-03-22 20:08:26

标签: vb.net

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

206.835-85.6*(Number of Syllables/Number of Words)-1.015*(Number of Words/Number of   Sentences)

以下是我遇到的问题:

  1. 我应该在多行文本框中显示文本文件的内容。
  2. 我应该在文本框下方的标签中显示我从函数indexCalculation得到的答案。
  3. 我无法调用该函数来实际让程序计算出要在标签中显示的答案。
  4. 这是我到目前为止的代码。

    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
    

    任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:1)

以下是我的建议:

  1. 更新indexCalculation函数以获取整数,而不是字符串。这样你就不必将它们转换为数字。
  2. 删除您未使用的所有额外变量。这样可以清理一下。
  3. 删除您的streamreader。看来你正在通过File.ReadAllText
  4. 阅读文本
  5. Label1.Show(answer)应更改为Label1.Show(indexCalculation(wordCount,sentenceCount,vowelCount)) - 除非Label1不是常规标签,否则使用Label1.Text = indexCalculation(wordCount,sentenceCount,vowelCount) ))
  6. 然后对于vowelCount,您需要执行以下操作:

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

    此外,将逻辑添加到CountSyllables函数,使其为0,如果为0.如果您不想在元音计数中包含最后一个字符,则使用for循环而不是每个循环的a并停止1个字符短。