Vb.net 计算单个文本框中数字的平均值vb

Vb.net 计算单个文本框中数字的平均值vb,vb.net,textbox,average,Vb.net,Textbox,Average,我需要把所有的数字放在一个文本框中,然后把它转换成一个平均数 我的代码: Dim dMods1 = "\\DirectPathToSpecificFolder" Dim dMods2 = "\\DirectPathToSpecificFolder" Dim dMods3 = "\\DirectPathToSpecificFolder" Dim dMods4 = "\\DirectPathToSpecificFolder" Dim fileCount1 As Integer = Director

我需要把所有的数字放在一个文本框中,然后把它转换成一个平均数

我的代码:

Dim dMods1 = "\\DirectPathToSpecificFolder"
Dim dMods2 = "\\DirectPathToSpecificFolder"
Dim dMods3 = "\\DirectPathToSpecificFolder"
Dim dMods4 = "\\DirectPathToSpecificFolder"

Dim fileCount1 As Integer = Directory.GetFiles(dMods1).Length
Dim fileCount2 As Integer = Directory.GetFiles(dMods2).Length
Dim fileCount3 As Integer = Directory.GetFiles(dMods3).Length
Dim fileCount4 As Integer = Directory.GetFiles(dMods4).Length

        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim array = TextBox2.Lines
    TextBox2.Clear()
    If Not dMods1 Is Nothing Then

        For Each FilePath As String In Directory.GetFiles(dMods1, "*.txt")
            TextBox2.Text &= System.IO.File.ReadAllText(FilePath) & vbNewLine
        Next
        For index = 0 To array.GetUpperBound(0)
            Debug.WriteLine(array)
        Next

    End If

End Sub
现在它将附带特定文件夹中的.txt文件的内容,并将其放在textbox2中。但它将与此
vbNewLine
一起出现,因此在数字之间创建空格

如何计算这些数字的平均值并将其放入
标签1

这里的课程应该是:

  • 编程不是你刚刚“学会”的东西
  • 大多数任务由几个较小的任务组成(迭代数组、获取总数、执行算术)
  • 学习新东西通常需要几个小时的研究(数组、迭代、数据类型、标记、调试…),以便编写10行代码
  • MSDN有很多远程教程,VS甚至有视频
从字符串数组计算平均值: 我假设文本框是代码中的多行,因此每个项目都有自己的行

' stand in for textbox lines
Dim Lines As String() = {"1", "3", "5", "2", "8"}
' the accumulator
Dim total As Int32
Dim tmp As Int32

For n As Int32 = 0 To Lines.Count - 1
    ' not all text cant be converted to numbers
    If Integer.TryParse(Lines(n), tmp) Then
        ' Lines(n) could convert to an integer
        ' accumulate the value
        Console.WriteLine("Value at {0} is {1}", n, tmp)
        total += tmp
    Else
        ' bad news!  Lines(n) cant convert!
    End If
Next

' whew! 
' Now the avg:
Dim intAvg = total \ Lines.Count - 1
Console.WriteLine("Integer average is {0}", intAvg)
' float/real version:
Dim fAvg = total / Lines.Count - 1
Console.WriteLine("Fractional average is {0}", fAvg)
这里的教训应该是:

  • 编程不是你刚刚“学会”的东西
  • 大多数任务由几个较小的任务组成(迭代数组、获取总数、执行算术)
  • 学习新东西通常需要几个小时的研究(数组、迭代、数据类型、标记、调试…),以便编写10行代码
  • MSDN有很多远程教程,VS甚至有视频
从字符串数组计算平均值: 我假设文本框是代码中的多行,因此每个项目都有自己的行

' stand in for textbox lines
Dim Lines As String() = {"1", "3", "5", "2", "8"}
' the accumulator
Dim total As Int32
Dim tmp As Int32

For n As Int32 = 0 To Lines.Count - 1
    ' not all text cant be converted to numbers
    If Integer.TryParse(Lines(n), tmp) Then
        ' Lines(n) could convert to an integer
        ' accumulate the value
        Console.WriteLine("Value at {0} is {1}", n, tmp)
        total += tmp
    Else
        ' bad news!  Lines(n) cant convert!
    End If
Next

' whew! 
' Now the avg:
Dim intAvg = total \ Lines.Count - 1
Console.WriteLine("Integer average is {0}", intAvg)
' float/real version:
Dim fAvg = total / Lines.Count - 1
Console.WriteLine("Fractional average is {0}", fAvg)

如果我理解正确,我想你应该有以下内容:

Dim dMods1 = "\\DirectPathToSpecificFolder"


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    If Not dMods1 Is Nothing Then
        For Each FilePath As String In Directory.GetFiles(dMods1, "*.txt")
            TextBox2.Text &= System.IO.File.ReadAllText(FilePath) & vbNewLine
        Next

        Dim arrayVariable As String = TextBox2.Text
        Dim sum As Integer = 0
        Dim count As Integer = 0
        For Each number In arrayVariable.Split(" ")
            Try
                sum += Convert.ToInt32(number)
                count += 1
            Catch 
            End Try
        Next

        Dim average As Double = sum / count
        Label1.Text = Convert.ToString(average)
    End If
End Sub

如果我理解正确,我想你应该有以下内容:

Dim dMods1 = "\\DirectPathToSpecificFolder"


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    If Not dMods1 Is Nothing Then
        For Each FilePath As String In Directory.GetFiles(dMods1, "*.txt")
            TextBox2.Text &= System.IO.File.ReadAllText(FilePath) & vbNewLine
        Next

        Dim arrayVariable As String = TextBox2.Text
        Dim sum As Integer = 0
        Dim count As Integer = 0
        For Each number In arrayVariable.Split(" ")
            Try
                sum += Convert.ToInt32(number)
                count += 1
            Catch 
            End Try
        Next

        Dim average As Double = sum / count
        Label1.Text = Convert.ToString(average)
    End If
End Sub


textBox.Lines
将为您提供一个数据数组,
textBox.Lines.Count-1)
将告诉您有多少行are@Plutonix如果我尝试说
Label1.Text=TextBox2.Lines
我会得到“类型为'1-dimensional array of String'的值无法转换为'String'”。但是
Count-1
非常有效……错误消息是正确的。正如我所说的,textBox.Lines将给你们一个数据数组,迭代(遍历)数组以获得数组中的值,好的,仍然可以遍历所有的术语(例如数组)。在再次查看之前,我会先搜索一下我自己,但是我该如何“迭代”数组呢。。。关于如何迭代
textBox有一个while小节。line
将为您提供一个数据数组,而
textBox.line.Count-1)
将告诉您有多少个are@Plutonix如果我尝试说
Label1.Text=TextBox2.Lines
我会得到“类型为'1-dimensional array of String'的值无法转换为'String'”。但是
Count-1
非常有效……错误消息是正确的。正如我所说的,textBox.Lines将给你们一个数据数组,迭代(遍历)数组以获得数组中的值,好的,仍然可以遍历所有的术语(例如数组)。在再次查看之前,我会先搜索一下我自己,但是我该如何“迭代”数组呢。。。有一个关于如何正确地迭代EIT的小节。但是我需要
标签1
中的结果。在你再次开始说“这不是一个辅导网站”之前,我自己试试;)感谢
Label1.Text=fAvg.ToString()
谢谢,这也很有效:)谢谢你,现在我对数组有了更好的理解记住数组中的第一个元素是0,所以当你迭代时,循环通过一个更少的(
Lines.Count-1
)大多数编程操作实际上是由几个较小的步骤组成的,这些步骤可以正常工作。但是我需要
标签1
中的结果。在你再次开始说“这不是一个辅导网站”之前,我自己试试;)感谢
Label1.Text=fAvg.ToString()
谢谢,这也很有效:)谢谢你,现在我对数组有了更好的理解记住数组中的第一个元素是0,所以当你迭代时,循环通过一个更少的(
Lines.Count-1
)大多数编程操作实际上是由几个较小的步骤组成的,使它与代码一起工作。谢谢但是arrayVariable.Split(“”)中每个数字的
必须更改为arrayVariable.Split(vbNewLine)
中每个数字的
,因为它用
vbNewLine
拆分。但非常感谢。不客气,我很高兴您理解了代码并对其进行了必要的更改,因此它将适合您的需要。使其与您的代码一起工作。谢谢但是arrayVariable.Split(“”)中每个数字的
必须更改为arrayVariable.Split(vbNewLine)
中每个数字的
,因为它用
vbNewLine
拆分。但非常感谢。不客气,我很高兴您理解了代码并对其进行了必要的更改,因此它将适合您的需要。