在此之前,让我承认它与我几天前提出的一个问题非常相似,关于其他几个问题,但在使用了我遇到的两个问题的答案之后,我现在正在运行进入另一个问题。此外,不确定标题实际上是否适合我遇到的问题,但似乎可能是代码在程序中的排序问题。
该计划的最终目标是根据添加到配料列表框的配方列表框中的成分以及每种选定成分的数量来计算配方中的卡路里数。我也应该执行几个不同的测试来防止程序崩溃。
我现在的主要问题是,无论我如何尝试调整代码以使其工作,我都无法使TotalCalories增加,所以它不断给我一个0值作为答案。我有点紧张,所以如果能够解决这个问题而不必重新编写我已经拥有的大部分代码,那就太棒了。
这是我写的代码
Public Class Form1
Private TotalCalories As Integer = 0
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim i As Integer = lstIngredients.SelectedIndex
Dim Quantity As Double
Dim intCount As Integer = 0
If Trim(txtQuantity.Text) = "" Then
Quantity = 1
Else
Quantity = Me.txtQuantity.Text
End If
If IsNumeric(txtQuantity.Text) = False Then
MessageBox.Show("The quantity entered is not numeric. Please add a numeric quantity.")
End If
If intCount < Quantity Then
lstRecipe.Items.Add(Quantity & " " & lstIngredients.Text)
intCount += 1
End If
If lstRecipe.Text = "Eggs(each)" Then
TotalCalories += Quantity * 72
ElseIf lstRecipe.Text = "Flour(cups)" Then
TotalCalories += Quantity * 455
ElseIf lstRecipe.Text = "Milk(cups)" Then
TotalCalories += Quantity * 86
ElseIf lstRecipe.Text = "Sugar(cups)" Then
TotalCalories += Quantity * 774
ElseIf lstRecipe.Text = "Butter(tablespoons)" Then
TotalCalories += Quantity * 102
End If
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
lstRecipe.Items.Clear()
txtQuantity.Clear()
txtAnswer.Clear()
TotalCalories = 0
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
txtAnswer.Text = TotalCalories
End Sub
End Class
我也试过这个 如果lstRecipe.text =(Quantity&amp;“”&amp;“Eggs”&amp;“”“(each)”)那么...... 但那也不起作用。
答案 0 :(得分:1)
lstRecipe.Text
将引用配方列表框中的SELECTED项,该项与您刚刚添加的项不同。即使选择了某些内容,您也可以在每次添加其他项目时添加许多卡路里。由于您要将(Quantity & " " & lstIngredients.Text)
发布到食谱,因此该文字永远不会与仅成分文本匹配(并且应该是Quantity.ToString
)。
lstRecipe.Items.Add(Quantity & " " & lstIngredients.Text)
' this assumes the quoted text is exactly
' what they contain. we cant see that, but
' lstRECIPE would be something like '4 Eggs(each)'
' this will also break if you change the text like
' add a space before the parens 'Eggs (each)'
' which is another reason a class is a much better way
If lstIngredients.Text = "Eggs(each)" Then
TotalCalories += Quantity * 72
ElseIf lstIngredients.Text = "Flour(cups)" Then
TotalCalories += Quantity * 455
ElseIf lstIngredients.Text = "Milk(cups)" Then
TotalCalories += Quantity * 86
ElseIf lstIngredients.Text = "Sugar(cups)" Then
TotalCalories += Quantity * 774
ElseIf lstIngredients.Text = "Butter(tablespoons)" Then
TotalCalories += Quantity * 102
End If
清洁剂:
lstRecipe.Items.Add(Quantity & " " & lstIngredients.Text)
Select Case lstIngredients.Text
Case "Eggs(each)"
TotalCalories += Quantity * 72
Case "Flour(cups)"
TotalCalories += Quantity * 455
Case "Milk(cups)" Then
TotalCalories += Quantity * 86
Case "Sugar(cups)"
TotalCalories += Quantity * 774
Case "Butter(tablespoons)"
TotalCalories += Quantity * 102
End Select
由于项目,数量和卡路里都需要在不同的位置,因此对于一个班级来说,这是理想。还有这些,因为这个解决方案尖叫'使用一个类'。