Frage

Before this gets flagged, let me admit that it is fairly similar to a question I asked a couple days ago, about a couple of other issues, but after using answers for both of the problems I was having, I am now running into another issue. Also, not sure that the title actually fits the problem I'm having, but it seems like it could be an issue of how the code is ordered in the program.

The eventual goal of this program is to calculate how many calories would be in a recipe based on ingredients added to a Recipe list box from an Ingredients list box, and the quantities of each selected ingredient. I am also supposed to perform a couple of different tests to prevent the program from crashing.

  1. If the quantity text box is left empty, the program is supposed to default to adding 1 of that selected ingredient.
  2. If anything other than a number is entered into the text box, the program is supposed to display a message box asking the user to please enter a numeric quantity.

My main problem now is that no matter how I try to tweak the code to make it work, I cannot get the TotalCalories to increment, so it keeps giving me a value of 0 as the answer. I am kind of on a time crunch, so if it's at all possible to fix this without having to re-write a big portion of the code I already have, that'd be great.

Here's the code I have written

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

I've also tried this using If lstRecipe.text= (Quantity & " " & "Eggs" & " " "(each)") Then ... But that didn't work either.

War es hilfreich?

Lösung

If txtQuantity is empty then you have the Quantity variable set to 1, else try to parse the value typed and return if it is not a number. Also use all integers for calcs or, if a decimal value is allowed, then use a double also for the TotalCalories variable.

Still it is not clear what you want to do if the user types a zero in the textbox

Private TotalCalories As Double = 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 if Not Double.TryParse(txtQuantity.Text, Quantity)
        MessageBox.Show("The quantity entered is not numeric. Please add a numeric quantity.")
        Return ' or Exit Sub
    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

However I think you need to check against the lstIngredients instead

    If lstIngredients.Text = "Eggs(each)" Then
      ....
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top