Vb.net visualbasic中变量的增量
在这被标记之前,让我承认这与我几天前提出的一个问题相当类似,关于其他几个问题,但是在使用了我之前遇到的两个问题的答案之后,我现在遇到了另一个问题。另外,我不确定标题是否符合我遇到的问题,但这似乎可能是程序中的代码排序问题 该计划的最终目标是根据从配料列表框添加到配方列表框的配料以及每个选定配料的数量,计算配方中的卡路里含量。我还应该执行几个不同的测试来防止程序崩溃Vb.net visualbasic中变量的增量,vb.net,increment,Vb.net,Increment,在这被标记之前,让我承认这与我几天前提出的一个问题相当类似,关于其他几个问题,但是在使用了我之前遇到的两个问题的答案之后,我现在遇到了另一个问题。另外,我不确定标题是否符合我遇到的问题,但这似乎可能是程序中的代码排序问题 该计划的最终目标是根据从配料列表框添加到配方列表框的配料以及每个选定配料的数量,计算配方中的卡路里含量。我还应该执行几个不同的测试来防止程序崩溃 如果数量文本框为空,则程序默认添加1种选定成分 如果文本框中输入的不是数字,则程序将显示一个消息框,要求用户输入数字量 我现在的主要
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
公共类表单1
私有TotalCarries作为整数=0
私有子btnAdd_Click(发送者作为对象,e作为事件参数)处理btnAdd。单击
Dim i作为整数=lstComponents.SelectedIndex
双倍数量
Dim intCount作为整数=0
如果Trim(txtQuantity.Text)=“”,则
数量=1
其他的
数量=Me.txtQuantity.Text
如果结束
如果IsNumeric(txtQuantity.Text)=False,则
MessageBox.Show(“输入的数量不是数字。请添加数字数量。”)
如果结束
如果intCount<数量,则
lstcemipe.Items.Add(数量和“&lstcomponents.Text)
整数计数+=1
如果结束
如果lstcemipe.Text=“每个鸡蛋”,则
总热量+=数量*72
ElseIf lstrepice.Text=“面粉(杯)”然后
总热量+=数量*455
ElseIf lstRecipe.Text=“牛奶(杯)”然后
总热量+=数量*86
ElseIf lstRecipe.Text=“糖(杯)”然后
总热量+=数量*774
ElseIf lstRecipe.Text=“黄油(汤匙)”然后
总热量+=数量*102
如果结束
端接头
私有子btnClear\u单击(发送者作为对象,e作为事件参数)处理btnClear。单击
lstrepic.Items.Clear()
txtQuantity.Clear()
txtAnswer.Clear()
总热量=0
端接头
私有子btnCalculate\u单击(发送者作为对象,e作为事件参数)处理btnCalculate。单击
txtAnswer.Text=总热量
端接头
末级
我也试过用这个
如果lstRecipe.text=(数量和“鸡蛋”和“每个”)则。。。
但这也不起作用。
lstrepice.Text
将引用配方列表框中的选定项,该项与您刚才添加的项不同。即使选择了某个项目,每次添加其他项目时,您也只需添加那么多卡路里。由于您正在将(数量和“&lstComponents.Text)
发布到配方,因此该文本永远不会与仅成分文本匹配(并且应该是Quantity.ToString
)
清洁剂:
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
由于项目、数量和热量都需要在不同的位置,这对于一个班级来说是理想的。这些选项是否已经出现,因为解决方案会尖叫“使用类”。在类的顶部,将选项设置为Strict On。您应该可以看到您正在使用的隐式转换,这些转换是您失败的原因。此外,您不需要使用
Trim(txtQuantity.Text)=“”
useString.IsNullOrWhitespace(txtQuantity.Text)
,也不需要初始化值类型(int)。VB将自动将它们初始化为默认值(值),如果是数字,则自动将其初始化为0。太棒了,感谢所有的建议@JimWooley@JimWooley我接受了你的建议,打开了Option Strict,现在在我程序的末尾,代码是txtAnswer.text=TotalCarries,我收到一个错误,声明“Option Strict on不允许从'Double'隐式转换为'String'。关于如何解决这个问题,有什么想法吗?非常感谢,它现在终于可以完美地工作了!
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