2017-02-15 123 views
0

我遇到了我的代码问题。我试图验证用户输入,所以只有数值而不是0,另一个条件是,dblFatInGrams不能大于dblCaloriesInFood。当我将这个语句添加到我的代码中时,它会直接执行else语句而不执行其余的代码。Visual Basic IF语句

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 
+1

我怀疑,编译器将保证评估从左到右。所以变量dblLowFoodPercent在你需要之前不会被TryParse填充。尝试使用“AndAlso”而不是“and”可能会使它工作,但它不是一个好的解决方案。总之,在你需要TryParse的值之前调用TryParse。我对这个结果非常感兴趣。 –

+0

问题在于''Double.TryParse()'语句正在'dblLowFoodPercent'上运行。这可能不是你想要的。 – Icemanind

+0

检查你的'TryParse'语句,它们都解析到同一个变量'dblLowFoodPercent'中。另外,把正确的标记,这是VB.net不是VBA。 –

回答

0

我已经更新了代码。

  1. 看来txtFoodCalories.Text应变量dblCaloriesInFood在1 If条件下被保留。
  2. 如果条件不正确,则为2nd。它使用一个常量dblLowFoodPercent。它应该是dblFatInGrams
  3. 删除IsNumericDouble.TryParse检查它。
  4. 删除了用户输入转换,因为TryParse已在执行此操作。

请尝试以下。

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 
+0

我确实更改了代码,但它是相同的问题。直接去Else语句并打印出“输入一个数字,不大于数字....”没有百分比输出空白标签。还将dblLowFoodPercent更改为dblLowFoodPercent,从而导致计算错误。 – Michael

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