2017-02-15 9 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によって記入されません。 "and"の代わりに "AndAlso"を使ってみてください。それはうまくいくかもしれませんが、それは素晴らしい解決策ではありません。 TryParseの値を必要とする前に、TryParseを短く呼び出します。私はこのことがどうなるかに非常に興味があります。 –

+0

問題は 'Double.TryParse()'ステートメントが 'dblLowFoodPercent'で動作していることです。これはおそらくあなたが望むものではありません。 – Icemanind

+0

'TryParse'ステートメントをチェックします。両方とも同じ変数' dblLowFoodPercent'に解析しています。また、正しいタグを入れてください。これはVBAではなくVB.netです。 –

答えて

0

私はコードを更新しました。

  1. txtFoodCalories.Textが第一If条件で変数dblCaloriesInFoodに保存されなければならないようです。
  2. 第2条件が間違っていた場合は第2です。定数dblLowFoodPercentを使用します。それはdblFatInGramsでなければなりません。
  3. IsNumericを削除しました。Double.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