Visual Basic IF语句

时间:2017-02-15 03:10:26

标签: vb.net

我的代码存在问题。我正在尝试验证用户输入,因此只是数值而不是0,另一个条件是dblFatInGrams不能大于dblCaloriesInFood。当我将这个语句添加到我的代码中时,它会直接转到else语句而不执行其余的代码。

Public Class Form1
    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

        Const intCaloriesPerGramoOfFat As Integer = 9 '9 calories per gram of fat
        Const dblLowFoodPercent As Double = 0.3       'low fat food are less then 30 %
        Dim dblCaloriesInFood As Double
        Dim dblFatInGrams As Double
        Dim intFatCalories As Integer
        Dim dblTotal As Double

        ' Validating user input and show message in lable box
        If (Double.TryParse(txtFoodCalories.Text, dblLowFoodPercent) And IsNumeric(txtFoodCalories.Text) And dblLowFoodPercent >= 0) Then
            If (Double.TryParse(txtFatGrams.Text, dblLowFoodPercent) And IsNumeric(txtFatGrams.Text) And dblLowFoodPercent >= 0) And dblCaloriesInFood < dblFatInGrams Then
                ' User input
                dblCaloriesInFood = CDbl(txtFoodCalories.Text)
                dblFatInGrams = CDbl(txtFatGrams.Text)

                'Calculate fat 
                intFatCalories = dblFatInGrams * intCaloriesPerGramoOfFat

                ' Calculate and dispaly % of cal. from fat
                dblTotal = intFatCalories / dblCaloriesInFood

                ' Dispaly results
                ' Check if fat percentage is grater or equal to 30% and prints out results
                If dblTotal <= dblLowFoodPercent Then
                    lblUserMessage.Text = "Low fat food"
                    lblTotal.Text = dblTotal.ToString("P")
                Else
                    lblUserMessage.Text = "High fat food"
                    lblTotal.Text = dblTotal.ToString("P")
                End If
            Else
                lblUserMessage.Text = "Enter a number of fat. Not grater than number of the calories and greater than 0"
            End If
        Else
            lblUserMessage.Text = "Enter the number of calories. Greater than 0 "
        End If
End Sub

2 个答案:

答案 0 :(得分:0)

我已经更新了代码。

  1. 似乎txtFoodCalories.Text应该在第一dblCaloriesInFood条件的变量If中保留。{/ li>
  2. 如果条件不正确,则为第2天。它使用常量dblLowFoodPercent。它应该是dblFatInGrams
  3. 删除IsNumeric Double.TryParse进行检查。
  4. 删除了用户输入转化,因为TryParse已经这样做了。
  5. 请尝试以下。

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Const intCaloriesPerGramoOfFat As Integer = 9 '9 calories per gram of fat
        Const dblLowFoodPercent As Double = 0.3       'low fat food are less then 30 %
        Dim dblCaloriesInFood As Double
        Dim dblFatInGrams As Double
        Dim intFatCalories As Integer
        Dim dblTotal As Double
    
        ' Validating user input and show message in lable box
        If (Double.TryParse(txtFoodCalories.Text, dblCaloriesInFood) And dblCaloriesInFood >= 0) Then
            If (Double.TryParse(txtFatGrams.Text, dblFatInGrams) And dblFatInGrams >= 0) And dblCaloriesInFood < dblFatInGrams Then
    
                'Calculate fat 
                intFatCalories = dblFatInGrams * intCaloriesPerGramoOfFat
    
                ' Calculate and dispaly % of cal. from fat
                dblTotal = intFatCalories / dblCaloriesInFood
    
                ' Dispaly results
                ' Check if fat percentage is grater or equal to 30% and prints out results
                If dblTotal <= dblLowFoodPercent Then
                    lblUserMessage.Text = "Low fat food"
                    lblTotal.Text = dblTotal.ToString("P")
                Else
                    lblUserMessage.Text = "High fat food"
                    lblTotal.Text = dblTotal.ToString("P")
                End If
            Else
                lblUserMessage.Text = "Enter a number of fat. Not grater than number of the calories and greater than 0"
            End If
        Else
            lblUserMessage.Text = "Enter the number of calories. Greater than 0 "
        End If
    End Sub
    

答案 1 :(得分:0)

看看这是否更好......

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Const intCaloriesPerGramoOfFat As Double = 9.0R     '9 calories per gram of fat
    Const dblLowFoodPercent As Double = 0.3R            'low fat food are less then 30 %

    Dim dblCaloriesInFood As Double = 0R
    Dim dblFatInGrams As Double = 0R

    Dim intFatCalories As Double = 0R
    Dim dblTotal As Double = 0R

    Select Case True
        Case Not Double.TryParse(txtFoodCalories.Text, dblCaloriesInFood), Not dblCaloriesInFood > 0
            lblUserMessage.Text = "Enter the number of calories. Greater than 0"
        Case Not Double.TryParse(txtFatGrams.Text, dblFatInGrams), Not dblFatInGrams > 0, Not dblFatInGrams < dblCaloriesInFood
            lblUserMessage.Text = "Enter a number of fat. Not grater than number of the calories and greater than 0"
        Case Else
            'Calculate fat 
            intFatCalories = dblFatInGrams * intCaloriesPerGramoOfFat

            ' Calculate and dispaly % of cal. from fat
            dblTotal = intFatCalories / dblCaloriesInFood
            lblTotal.Text = dblTotal.ToString("P")

            ' Check if fat percentage is grater or equal to 30% and prints out results
            If dblTotal <= dblLowFoodPercent Then
                lblUserMessage.Text = "Low fat food"
            Else
                lblUserMessage.Text = "High fat food"
            End If
    End Select
End Sub

Screenshot