在我为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
另外,这是我可以使用数据集的实例吗?我以前从未使用过它们,所以不知道它是如何工作的,但是如果它可以使它变得更简单,我将全力以赴。
但是我不知道如何将热量值与各个项目联系起来。
由于卡路里的量将取决于每种成分的量,因此代表一种成分的类将是有帮助的:
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
现在,如果您有一个List(Of Ingredient)
被调用的Ingredients
话,那么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覆盖来显示数据,然后很简单地显示卡路里总量。
这种方法的优势在于,列表框项目实际上是成分对象。因此,更改一种成分的数量很简单,只需将所选项目转换为成分类型并更改数量即可。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句