如何修复此 VB 代码? [未为参数指定参数]
How do I fix this VB code? [Argument not specified for parameter]
我正在努力让这段代码工作:
Module Module1
Function BMI(ByVal H, ByVal W)
BMI = (W / H) ^ 2
Return BMI
End Function
Function reading(ByVal BMI)
If BMI <= 19 Then
reading = "Underweight"
ElseIf BMI >= -20 <= 25 Then
reading = "Perfect"
ElseIf BMI >= 26 <= 30 Then
reading = "Slightly Overweight"
ElseIf BMI >= 31 <= 40 Then
reading = "Overweight"
ElseIf BMI >= 41 Then
reading = "Obese"
End If
Return reading
End Function
Sub Main()
Dim height, weight As Integer
Console.Write("What is your height? ")
height = Console.ReadLine
Console.Write("What is your weight? ")
weight = Console.ReadLine
BMI(height, weight)
reading(BMI)
End Sub
End Module
我得知 'reading(BMI)' 有一个参数 'W' 的 'Public Function BMI(H As Object, W As Object) As Object.'
没有指定
有人可以帮我解决这个问题吗?
为了将BMI
-函数的结果交给reading
方法,需要将结果存储在变量中,而不是使用函数名BMI
再次(相关更改包含在最后两行中):
Sub Main()
Dim height, weight As Integer
Console.Write("What is your height? ")
height = Console.ReadLine
Console.Write("What is your weight? ")
weight = Console.ReadLine
Dim result = BMI(height, weight)
reading(result)
End Sub
除了 Markus 写的:
删除函数本身中的 "return" 语句。我不相信 vba 喜欢那里的那些。
您已将值分配给函数名称..这就是您需要做的:
所以删除:
Return BMI
Return reading
这里是你的代码重构了一点。它没有对用户输入进行任何错误检查。如果您使用的是公制单位,请删除公式中的 * 703
部分:
Module Module1
Sub Main()
Dim height, weight As Integer
Console.Write("What is your height in inches? ")
height = Console.ReadLine
Console.Write("What is your weight in pounds? ")
weight = Console.ReadLine
Dim BMI As Integer = CalculateBMI(height, weight)
Dim Category As String = reading(BMI)
Console.WriteLine(String.Format("Your BMI is {0}, which is {1}.", BMI, Category))
Console.Write("Press Enter to Quit")
Console.ReadLine()
End Sub
Function CalculateBMI(ByVal H As Integer, ByVal W As Integer) As Integer
Return CDbl(W * 703) / Math.Pow(H, 2)
End Function
Function reading(ByVal BMI As Integer) As String
Select Case BMI
Case Is <= 19
Return "Underweight"
Case 20 To 25
Return "Perfect"
Case 26 To 30
Return "Slightly Overweight"
Case 31 To 40
Return "Overweight"
Case Else
Return "Obese"
End Select
End Function
End Module
有几处错误。试试我下面的内容,看看它是否像您预期的那样工作。我对我所做的更改的代码进行了评论。
Module Module1
Function BMI(ByVal H As Integer, ByVal W As Integer) As Integer ' set a return type and specify the parameter types
BMI = (W / H) ^ 2
' Dont need this here, you can return the value by simply assinging the function name the return value
' Return BMI
End Function
Function reading(ByVal _BMI As Integer) As String ' set a return type and specify the parameter types
'This was not the correct syntax for an If statement.
If _BMI <= 19 Then
reading = "Underweight"
ElseIf _BMI >= 20 And _BMI <= 25 Then ' ElseIf BMI >= -20 <= 25 Then ' Also killed the -20 and made it just 20
reading = "Perfect"
ElseIf _BMI >= 26 And _BMI <= 30 Then ' ElseIf BMI >= 26 <= 30 Then
reading = "Slightly Overweight"
ElseIf _BMI >= 31 And _BMI <= 40 Then ' ElseIf BMI >= 31 <= 40 Then
reading = "Overweight"
ElseIf _BMI >= 41 Then
reading = "Obese"
Else
reading = "Unknown" ' Just in case...
End If
' Again, don't need this here
' Return reading
End Function
Sub Main()
Dim height, weight As Integer
Try
Console.Write("What is your height? ")
height = Integer.Parse(Console.ReadLine) ' make sure it's an integer
Console.Write("What is your weight? ")
weight = Integer.Parse(Console.ReadLine) ' make sure it's an integer
Dim result As String = reading(BMI(height, weight)) ' Need to feed the reading function an actual value. Before it was trying to call the BMI function with no parameters hence why it was failing.
Console.Write(String.Format("Your result is: {0}", result)) ' Output
Catch ex As Exception
Console.WriteLine(ex.Message)
Console.ReadLine()
End Try
End Sub
End Module
我正在努力让这段代码工作:
Module Module1
Function BMI(ByVal H, ByVal W)
BMI = (W / H) ^ 2
Return BMI
End Function
Function reading(ByVal BMI)
If BMI <= 19 Then
reading = "Underweight"
ElseIf BMI >= -20 <= 25 Then
reading = "Perfect"
ElseIf BMI >= 26 <= 30 Then
reading = "Slightly Overweight"
ElseIf BMI >= 31 <= 40 Then
reading = "Overweight"
ElseIf BMI >= 41 Then
reading = "Obese"
End If
Return reading
End Function
Sub Main()
Dim height, weight As Integer
Console.Write("What is your height? ")
height = Console.ReadLine
Console.Write("What is your weight? ")
weight = Console.ReadLine
BMI(height, weight)
reading(BMI)
End Sub
End Module
我得知 'reading(BMI)' 有一个参数 'W' 的 'Public Function BMI(H As Object, W As Object) As Object.'
没有指定有人可以帮我解决这个问题吗?
为了将BMI
-函数的结果交给reading
方法,需要将结果存储在变量中,而不是使用函数名BMI
再次(相关更改包含在最后两行中):
Sub Main()
Dim height, weight As Integer
Console.Write("What is your height? ")
height = Console.ReadLine
Console.Write("What is your weight? ")
weight = Console.ReadLine
Dim result = BMI(height, weight)
reading(result)
End Sub
除了 Markus 写的:
删除函数本身中的 "return" 语句。我不相信 vba 喜欢那里的那些。 您已将值分配给函数名称..这就是您需要做的:
所以删除:
Return BMI
Return reading
这里是你的代码重构了一点。它没有对用户输入进行任何错误检查。如果您使用的是公制单位,请删除公式中的 * 703
部分:
Module Module1
Sub Main()
Dim height, weight As Integer
Console.Write("What is your height in inches? ")
height = Console.ReadLine
Console.Write("What is your weight in pounds? ")
weight = Console.ReadLine
Dim BMI As Integer = CalculateBMI(height, weight)
Dim Category As String = reading(BMI)
Console.WriteLine(String.Format("Your BMI is {0}, which is {1}.", BMI, Category))
Console.Write("Press Enter to Quit")
Console.ReadLine()
End Sub
Function CalculateBMI(ByVal H As Integer, ByVal W As Integer) As Integer
Return CDbl(W * 703) / Math.Pow(H, 2)
End Function
Function reading(ByVal BMI As Integer) As String
Select Case BMI
Case Is <= 19
Return "Underweight"
Case 20 To 25
Return "Perfect"
Case 26 To 30
Return "Slightly Overweight"
Case 31 To 40
Return "Overweight"
Case Else
Return "Obese"
End Select
End Function
End Module
有几处错误。试试我下面的内容,看看它是否像您预期的那样工作。我对我所做的更改的代码进行了评论。
Module Module1
Function BMI(ByVal H As Integer, ByVal W As Integer) As Integer ' set a return type and specify the parameter types
BMI = (W / H) ^ 2
' Dont need this here, you can return the value by simply assinging the function name the return value
' Return BMI
End Function
Function reading(ByVal _BMI As Integer) As String ' set a return type and specify the parameter types
'This was not the correct syntax for an If statement.
If _BMI <= 19 Then
reading = "Underweight"
ElseIf _BMI >= 20 And _BMI <= 25 Then ' ElseIf BMI >= -20 <= 25 Then ' Also killed the -20 and made it just 20
reading = "Perfect"
ElseIf _BMI >= 26 And _BMI <= 30 Then ' ElseIf BMI >= 26 <= 30 Then
reading = "Slightly Overweight"
ElseIf _BMI >= 31 And _BMI <= 40 Then ' ElseIf BMI >= 31 <= 40 Then
reading = "Overweight"
ElseIf _BMI >= 41 Then
reading = "Obese"
Else
reading = "Unknown" ' Just in case...
End If
' Again, don't need this here
' Return reading
End Function
Sub Main()
Dim height, weight As Integer
Try
Console.Write("What is your height? ")
height = Integer.Parse(Console.ReadLine) ' make sure it's an integer
Console.Write("What is your weight? ")
weight = Integer.Parse(Console.ReadLine) ' make sure it's an integer
Dim result As String = reading(BMI(height, weight)) ' Need to feed the reading function an actual value. Before it was trying to call the BMI function with no parameters hence why it was failing.
Console.Write(String.Format("Your result is: {0}", result)) ' Output
Catch ex As Exception
Console.WriteLine(ex.Message)
Console.ReadLine()
End Try
End Sub
End Module