在列表框中的一行中读取多个整数 [英] Reading more than one integer on a line in a listbox

查看:21
本文介绍了在列表框中的一行中读取多个整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个按钮可以读取文件,并将该文件的内容放入列表框中.当我按下按钮时,显示的是:

So I have a button which reads a file, and puts the contents of that file into a list box. When I press the button this is what is shows:

吉姆 6 8 9

蒂姆 7 5 6

比尔 4 10 8

我想做的是制作一个单独的按钮,将每个人的分数相加,然后找到它们的平均值.一旦它计算了这个人的平均值,那么我希望平均值代替 3 个分数.

What I want to do is make a separate button which adds each of the person's scores and then finds the average of them. Once it has calculated the average of the person then I want the average to be in the place of the 3 scores.

我目前的代码只取每个人的第一个分数,然后将所有分数相加并在消息框中显示结果.

The code that I have at the moment only takes the first score of each person and then adds all of them and shows the result in a messagebox.

这是我目前的代码:

Dim scorevalues As New List(Of Integer)
    For Each line As String In System.IO.File.ReadLines(file1)
        Dim scores As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(line, "\d+")
        If scores.Success Then
            scorevalues.Add(Convert.ToInt32(scores.Value))
        End If
    Next
    listbox1.DataSource = scorevalues

    Dim Scoretots As Integer = 0

    For scores2 = 0 To listbox1.Items.Count - 1
        Scoretots = Scoretots + listbox.Items(scores2)
    Next
    MessageBox.Show("Total: " & Scoretots.ToString)

这是我的代码产生的:

6

7

4

然后一个消息框显示 28

And then a messagebox shows 28

推荐答案

这会让你继续前进.它与这个问题相同 https://stackoverflow.com/questions/29902255/working-out-averages-of-numbers-in-a-list-box-line/29902583#29902583

This will get you going. It's the same as this question https://stackoverflow.com/questions/29902255/working-out-averages-of-numbers-in-a-list-box-line/29902583#29902583

Sub Main()
    Dim scores As String = "Bill 10 9 8"
    Dim score As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(scores, "\d+")

    Dim sum As Integer = 0
    For i As Integer = 0 To score.Count - 1
        sum += Convert.ToInt32(score.Item(i).Value)
        Console.WriteLine(score.Item(i).Value)
    Next
    Dim average = sum / score.Count
    Console.WriteLine("Average: {0}", average)

    Console.ReadLine()
End Sub

结果:

这篇关于在列表框中的一行中读取多个整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆