tomatomako
tomatomako

Reputation: 3

Why is my if statement only returning the second result?

My calculations work but every time I run the program my output will be the "negative, does not exceed" result. I'm very new to all this so please any suggestions will help.

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim foodname As String = MaskedTextBox1.Text
        Dim calories As Integer = MaskedTextBox2.Text
        Dim fat As Integer = MaskedTextBox3.Text
        Dim calculation As Double = (fat * 9) / calories
        Dim positive As String = "which exceeds AHA recommendation"
        Dim negative As String = "which does not exceed AHA recommendation"
        ListBox1.Items.Clear()

        If ((fat * 9) / calories) > (0.3 * 100) Then
            ListBox1.Items.Add(foodname & " contains " & FormatPercent(calculation) & " calories from fat, " & positive)
        ElseIf ((fat * 9) / calories) < (0.29 * 100) Then
            ListBox1.Items.Add(foodname & " contains " & FormatPercent(calculation) & " calories from fat, " & negative)
        End If
    End Sub
End Class

Upvotes: 0

Views: 43

Answers (1)

Mirronelli
Mirronelli

Reputation: 780

would this help?

    If ((fat * 9) / calories) > (0.3 * 100) Then
        ListBox1.Items.Add(foodname & " contains " & FormatPercent(calculation) & " calories from fat, " & positive)
    Else
        ListBox1.Items.Add(foodname & " contains " & FormatPercent(calculation) & " calories from fat, " & negative)
    End If

you have only two options here, no need for 2 conditions. either the first one is true, or it must be the second one

Upvotes: 1

Related Questions