2014-02-21 47 views
2

在這個問題被標記之前,讓我承認它與我前幾天提問的一個問題非常類似,涉及到其他一些問題,但在對這兩個問題使用答案後有,我現在遇到另一個問題。另外,不確定標題實際上是否適合我遇到的問題,但它似乎可能是代碼在程序中的排序問題。Visual Basic中變量的增量

該計劃的最終目標是根據成分添加到成分列表框中的配方列表框以及每種選定成分的數量,計算配方中的卡路里量。我也應該執行一些不同的測試來防止程序崩潰。

  1. 如果數量文本框留空,程序應該默認添加1個選定的成分。
  2. 如果在文本框中輸入了數字以外的任何內容,程序應該顯示一個消息框,要求用戶輸入數字數量。

現在我的主要問題是,無論我如何調整代碼來使其工作,我無法獲得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 =(數量& 「」 & 「雞蛋」 & 「」 「(每個)」)隨後也試過這個。 .. 但這也沒有奏效。

+0

在班級的頂部,設置選項嚴格打開。您應該看到您使用的隱式轉換是您失敗的根源。 –

+0

而不是'Trim(txtQuantity.Text)=「」'使用'String.IsNullOrWhitespace(txtQuantity.Text)' –

+0

而不需要初始化值類型(整數)。 VB會自動初始化它們作爲默認值(值)或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 

由於都需要不同的斑點項目,數量和熱量,這是一類理想。有沒有這些,因爲這個解決方案尖叫「使用一個類」。

+0

謝謝sooooo多,它終於現在完美的工作! –