You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB BMI计算器开发求助:如何添加全体用户平均体重计算功能

How to Add a "Average Weight of All Users" Feature to Your VB BMI Calculator

Hey there! Let's work through adding that average weight feature you need, since you already have the BMI calculator core and a login system storing usernames/passwords as strings. Here's a step-by-step approach tailored to your setup:

1. Update Your User Data Storage to Include Weight

Right now, you're only storing usernames and passwords as strings—you'll need to extend this to save each user's weight too. If you're using a text file (common for simple VB apps), adjust your storage format to include weight with a delimiter (like | or ,). For example:

  • Old format: username1|password1
  • New format: username1|password1|75.5 (where 75.5 is the user's weight in kg)

When a user registers or calculates their BMI, make sure to save/update their weight in this format. Here's a quick snippet for saving a user's data:

' Example: Save user data to a text file
Dim userData As String = $"{txtUsername.Text}|{txtPassword.Text}|{txtWeight.Text}"
File.AppendAllText("users.txt", userData & Environment.NewLine)

2. Write Code to Calculate the Average Weight

Next, you'll need to read all user records from your storage, extract the weight values, and compute the average. Here's a VB function that does this:

Function CalculateAverageWeight() As Double
    Dim totalWeight As Double = 0
    Dim userCount As Integer = 0
    
    ' Check if the user file exists first
    If File.Exists("users.txt") Then
        Dim allUsers As String() = File.ReadAllLines("users.txt")
        
        For Each userLine As String In allUsers
            ' Split each line into parts using the delimiter
            Dim userParts As String() = userLine.Split("|"c)
            
            ' Make sure the line has a weight value
            If userParts.Length >= 3 Then
                Dim weight As Double
                If Double.TryParse(userParts(2), weight) Then
                    totalWeight += weight
                    userCount += 1
                End If
            End If
        Next
    End If
    
    ' Avoid division by zero if there are no users
    If userCount = 0 Then
        Return 0
    Else
        Return totalWeight / userCount
    End If
End Function

3. Display the Average Weight in Your UI

Once you have the average, show it to the user—maybe add a label or a text box to your form. Call the function when needed (like when a user logs in, or when a new BMI calculation is saved):

' Example: Show average weight in a label
lblAverageWeight.Text = $"Average User Weight: {CalculateAverageWeight():F1} kg"

4. Edge Cases to Consider

  • No users registered: The function returns 0, so you might want to show a message like "No users found" instead of 0 to avoid confusion.
  • Invalid weight values: Using Double.TryParse ensures that malformed weight entries don't break your calculation.
  • Updating user weight: If you want to update a user's weight when they re-calculate their BMI, you'll need to find their existing line in the file, replace it with the new weight, and rewrite the file. Here's a quick snippet for that:
' Update a logged-in user's weight
Dim allUsers As List(Of String) = File.ReadAllLines("users.txt").ToList()
For i As Integer = 0 To allUsers.Count - 1
    Dim userParts As String() = allUsers(i).Split("|"c)
    If userParts(0) = currentLoggedInUsername Then ' Replace with your logged-in user variable
        allUsers(i) = $"{userParts(0)}|{userParts(1)}|{txtWeight.Text}"
        Exit For
    End If
Next
File.WriteAllLines("users.txt", allUsers)

That should cover the core of what you need! Let me know if you need help adapting this to your specific login system or storage method.

内容的提问来源于stack exchange,提问作者Senpai666

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:58:02