【问题标题】:Calculations in Visual BasicVisual Basic 中的计算
【发布时间】:2014-02-19 02:10:48
【问题描述】:

在我正在为我的 Visual Basic 课程编写的程序中,我将编写一个程序来计算食谱中有多少卡路里。

我的表单有 2 个列表框(lstIngredients 和 lstRecipe)和一个数量文本框(txtQuantity),以及一个计算卡路里的按钮(btnCalculate)。还有其他事情,但我列出的这些是唯一与这个问题相关的事情。

我已经编写了代码以将具有适当数量的选定项目添加到食谱列表框中,但我对如何计算卡路里感到困惑。

成分列表框中有以下项目:鸡蛋(每个)、面粉(杯)、牛奶(杯)、糖(杯)和黄油(汤匙)。根据说明,我们获得了这些物品的卡路里:1 个鸡蛋 = 72 卡路里,1 杯面粉 = 455 卡路里,1 杯牛奶 = 86 卡路里,1 杯糖 = 774 卡路里,1 汤匙黄油 = 102 卡路里。当用户单击“计算卡路里”按钮时,程序应该使用这些值、添加到食谱列表的项目及其数量来计算该食谱中的卡路里总数。

我了解在此进行的数学运算,如果食谱需要 2 个鸡蛋、3 杯面粉和 2 杯牛奶,我必须将每种成分的卡路里乘以数量,然后添加所有这些值加在一起就得到了食谱的总卡路里。但我不知道如何将热量值与单个项目联系起来。

这是我目前编写的代码。

Public Class Form1

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim i As Integer = lstIngredients.SelectedIndex
        Dim Quantity As Integer

        If Trim(txtQuantity.Text = "") Then
            Quantity = 1
        Else
            Quantity = Me.txtQuantity.Text
        End If

        Dim intCount As Integer = 0
        While intCount < Quantity
            lstRecipe.Items.Add(lstIngredients.Text)
            intCount += 1
        End While
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        lstRecipe.Items.Clear()
        txtQuantity.Clear()
        txtAnswer.Clear()
    End Sub

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles  btnCalculate.Click
        Dim dblCalories As Double

        If txtQuantity.Text = "" Then
            txtAnswer.Text = "0 Calories"
        End If

    End Sub
End Class

另外,这是我可以使用数据集的实例吗?我以前从未使用过它们,所以不知道它是如何工作的,但如果它能让这个更简单,我完全赞成哈。

【问题讨论】:

  • 对名称和卡路里使用一个类,并可能覆盖 ToString。这样,每种成分都可以将相关信息保存在一起,也可以存储在 LstBox 中。

标签: vb.net


【解决方案1】:

但我不知道如何将热量值与个人联系起来 项目。

由于卡路里的数量取决于每种成分的数量,因此表示一种成分的类会很有帮助:

Class Ingredient
    Public Property Name As String = ""
    Public Property Amount As Double = 0
    Public Property Measure As Measurements = Measurements.Tsp
    Private Property Calories As Double = 0
    Private Property CalMeas As Measurements = Measurements.Tsp
    Public Sub New(_name As String, _calories As Double, _caloriemeasure As Measurements, Optional _amount As Double = 0, Optional _measure As Measurements = Measurements.Tsp)
        Name = _name
        Calories = _calories
        CalMeas = _caloriemeasure
        Amount = _amount
        Measure = _measure
    End Sub
    Public Function GetCalories() As Double
        Return (Amount * Measure) * (Calories / CalMeas)
    End Function
    Public Overrides Function ToString() As String
        Return Join({Name.PadRight(10, " "c), (Amount.ToString().PadRight(4, " "c) + [Enum].GetName(GetType(Measurements), Measure)).PadRight(10, " "c), GetCalories.ToString.PadLeft(5, " "c)}, vbTab)
    End Function
End Class
Enum Measurments
    Unit = 1
    Tsp = 1
    Tbsp = 3
    Cup = 48
End Enum

现在,如果您有一个名为 IngredientsList(Of Ingredient),那么 Sum 方法中的一个简单 lambda 将为您提供卡路里总量,Ingredients.Sum(Function(x) x.GetCalories)

Dim Ingredients As New List(Of Ingredient)(
    {
        New Ingredient("Flour", 455, Measurements.Cup, 3, Measurements.Cup),
        New Ingredient("Milk", 86, Measurements.Cup, 2, Measurements.Cup),
        New Ingredient("Sugar", 774, Measurements.Cup, 0.5, Measurements.Cup),
        New Ingredient("Egg", 72, Measurements.Unit, 2, Measurements.Unit),
        New Ingredient("Butter", 102, Measurements.Tbsp, 1, Measurements.Cup)
    })

 Dim total = Ingredients.Sum(Function(x) x.GetCalories)'3700.0

构造函数的设置是为了让您传递成分的名称、每个度量的卡路里数量以及可选的数量和度量。

通过将列表设置为列表框的数据源,列表框将使用ToString覆盖来显示数据,那么显示卡路里总量就很简单了。

这种方法的优点是列表框项实际上是成分对象。因此,更改一种成分的数量很简单,只需将所选项目转换为成分类型并更改数量即可。

【讨论】:

    【解决方案2】:

    你可以创建一个

    Dictionary<string,int> 
    

    (对不起 c#)然后将每种成分添加到字典中,使用成分的名称作为键,卡路里作为值。然后,您可以通过查找密钥轻松匹配。

    编辑(感谢 davidsbro 的评论):

    类似:

    Dictionary<string,int> dIngredients = new Dictionary<string, int>();
    
    // Add the code here to add the ingredients
    
    foreach(string s in lstIngredients)
    {
        dblCalories += dIngredients[s];
    }
    

    【讨论】:

    • 你为什么不用 developerfusion 或类似的东西把它转换成 vb 呢? :)
    猜你喜欢
    • 2017-03-05
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多