Visual Basic中语句结束时出现的预期错误

时间:2014-02-20 02:05:53

标签: vb.net compiler-errors

我一直在为我正在处理的程序的几行代码中出现此错误。我不知道我做错了什么,或者如何解决它。

以下是给我麻烦的代码部分。

    If lstRecipe.Text = "Eggs(each)" Then
        dblTotalCalories = dblTotalCalories += dblQuantity * 72
    ElseIf lstRecipe.Text = "Flour(cups)" Then
        dblTotalCalories = dblTotalCalories += dblQuantity * 455
    ElseIf lstRecipe.Text = "Milk(cups)" Then
        dblTotalCalories = dblTotalCalories += dblQuantity * 86
    ElseIf lstRecipe.Text = "Sugar(cups)" Then
        dblTotalCalories = dblTotalCalories += dblQuantity * 774
    ElseIf lstRecipe.Text = "Butter(tablespoons)" Then
        dblTotalCalories = dblTotalCalories += dblQuantity * 102
    End If

错误特别发生在每行的+ = dblQuantity * ...部分。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:4)

你正在混合两种不同的方式来做同样的事情。你的代码应该是 dblTotalCalories += dblQuantity * 72

 dblTotalCalories = dblTotalCalories + dblQuantity * 72

答案 1 :(得分:3)

您的语法无效:

dblTotalCalories = dblTotalCalories += dblQuantity * 72

这应该是

dblTotalCalories += dblQuantity * 72

这同样适用于所有其他实例。

答案 2 :(得分:1)

If lstRecipe.Text = "Eggs(each)" Then
     dblTotalCalories += dblQuantity * 72
ElseIf lstRecipe.Text = "Flour(cups)" Then
    dblTotalCalories += dblQuantity * 455
ElseIf lstRecipe.Text = "Milk(cups)" Then
    dblTotalCalories += dblQuantity * 86
ElseIf lstRecipe.Text = "Sugar(cups)" Then
     dblTotalCalories += dblQuantity * 774
ElseIf lstRecipe.Text = "Butter(tablespoons)" Then
    dblTotalCalories += dblQuantity * 102
End If