VB - 负数和条件语句
VB - Negative Number and Conditional Statement
又回来了,另一个菜鸟问题让我很烦。我花了 2 天时间试图解决这个问题。看来我和VB还有负数总是有误会
除了条件语句,如果两个文本框字符串的双重转换小于零,则应该打印出错误消息的条件语句除外,我得到了程序中的所有其他内容,以便脂肪百分比计算器工作。然而,即使我在测试时为两者都输入了负数,程序也会跳过我的 Else 错误语句并计算这两个负数并得到一些完全荒谬的脂肪百分比数字。如果我计算的代码的 "If-Then" 部分与百分比答案不匹配,它甚至看起来好像只是在处理表达式。
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
'Declare variables
Dim dblCaloriesInFood As Double
Dim dblGramsOfFat As Double
Dim dblPercentageOfCaloriesFromFat As Double
Dim dblCaloriesFromFat As Double
'Always initialize lblLowInFat message as not visible when button is clicked
lblLowInFat.Visible = False
Try
'Converting textbox strings to double.
dblCaloriesInFood = CDbl(txtCaloriesInFood.Text)
dblGramsOfFat = CDbl(txtGramsOfFat.Text)
If dblCaloriesInFood Or dblGramsOfFat > 0 Then
'Calculate Calories from fat
dblCaloriesFromFat = dblGramsOfFat * 9
'Calculate percentage of calories from fat
dblPercentageOfCaloriesFromFat = dblCaloriesFromFat / dblCaloriesInFood
'Display percentage of calories from fat
If dblPercentageOfCaloriesFromFat >= 0 Then
lblMessage.Text = dblPercentageOfCaloriesFromFat.ToString("p")
Else
lblMessage.Text = String.Empty
End If
'Display low fat message
If dblPercentageOfCaloriesFromFat <= 0.3 And dblPercentageOfCaloriesFromFat > 0 Then
lblLowInFat.Visible = True
End If
Else
'really tried to make this message work but couldn't figure it out.
'why does it only display this message when zero is entered and not when
'negative numbers are entered. instead it just calculates the negative numbers
'as if they were whole positive numbers or something, not sure because the percentage
'is way off the charts when i enter negative numbers. can't figure this out.
MessageBox.Show("Either the calories or fat grams were incorrectly entered")
txtCaloriesInFood.Text = String.Empty
txtGramsOfFat.Text = STring.Empty
txtCaloriesInFood.Focus()
End If
Catch
'error message for invalid input
MessageBox.Show("Please enter a numeric value for calories in food & number of fat grams.")
txtCaloriesInFood.Focus()
End Try
End Sub
您的 If
陈述是错误的。首先,您需要分别测试这两个值。其次,您希望两个值都大于零,因此您应该使用 And
而不是 Or
(这意味着只有一个值必须大于零)。
If dblCaloriesInFood > 0 And dblGramsOfFat > 0 Then
又回来了,另一个菜鸟问题让我很烦。我花了 2 天时间试图解决这个问题。看来我和VB还有负数总是有误会
除了条件语句,如果两个文本框字符串的双重转换小于零,则应该打印出错误消息的条件语句除外,我得到了程序中的所有其他内容,以便脂肪百分比计算器工作。然而,即使我在测试时为两者都输入了负数,程序也会跳过我的 Else 错误语句并计算这两个负数并得到一些完全荒谬的脂肪百分比数字。如果我计算的代码的 "If-Then" 部分与百分比答案不匹配,它甚至看起来好像只是在处理表达式。
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
'Declare variables
Dim dblCaloriesInFood As Double
Dim dblGramsOfFat As Double
Dim dblPercentageOfCaloriesFromFat As Double
Dim dblCaloriesFromFat As Double
'Always initialize lblLowInFat message as not visible when button is clicked
lblLowInFat.Visible = False
Try
'Converting textbox strings to double.
dblCaloriesInFood = CDbl(txtCaloriesInFood.Text)
dblGramsOfFat = CDbl(txtGramsOfFat.Text)
If dblCaloriesInFood Or dblGramsOfFat > 0 Then
'Calculate Calories from fat
dblCaloriesFromFat = dblGramsOfFat * 9
'Calculate percentage of calories from fat
dblPercentageOfCaloriesFromFat = dblCaloriesFromFat / dblCaloriesInFood
'Display percentage of calories from fat
If dblPercentageOfCaloriesFromFat >= 0 Then
lblMessage.Text = dblPercentageOfCaloriesFromFat.ToString("p")
Else
lblMessage.Text = String.Empty
End If
'Display low fat message
If dblPercentageOfCaloriesFromFat <= 0.3 And dblPercentageOfCaloriesFromFat > 0 Then
lblLowInFat.Visible = True
End If
Else
'really tried to make this message work but couldn't figure it out.
'why does it only display this message when zero is entered and not when
'negative numbers are entered. instead it just calculates the negative numbers
'as if they were whole positive numbers or something, not sure because the percentage
'is way off the charts when i enter negative numbers. can't figure this out.
MessageBox.Show("Either the calories or fat grams were incorrectly entered")
txtCaloriesInFood.Text = String.Empty
txtGramsOfFat.Text = STring.Empty
txtCaloriesInFood.Focus()
End If
Catch
'error message for invalid input
MessageBox.Show("Please enter a numeric value for calories in food & number of fat grams.")
txtCaloriesInFood.Focus()
End Try
End Sub
您的 If
陈述是错误的。首先,您需要分别测试这两个值。其次,您希望两个值都大于零,因此您应该使用 And
而不是 Or
(这意味着只有一个值必须大于零)。
If dblCaloriesInFood > 0 And dblGramsOfFat > 0 Then