我该如何修复这个VB代码? [参数未指定参数]

时间:2015-02-03 16:56:17

标签: vb.net function arguments procedures

我努力让这段代码发挥作用:

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

我已经通知了阅读(BMI)&#39;有一个&#39;参数未指定参数&#39; W&#39; &#39;公共功能BMI(H As Object,W As Object)As Object。&#39;

有人可以帮我解决这个问题吗?

4 个答案:

答案 0 :(得分:1)

为了将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

答案 1 :(得分:1)

这是你的代码重新考虑了一下。它没有对用户输入进行任何错误检查。如果您使用的是公制单位,请删除公式中的* 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

答案 2 :(得分:0)

有几件事是错的。试试我下面的内容,看看它是否像您期望的那样有效。我对我所做的更改进行了评论。

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

答案 3 :(得分:-1)

除了马库斯所写的内容外:

删除函数本身的“return”语句。我不相信vba喜欢那些。 您已将值分配给函数名称..这就是您需要做的所有事情:

所以删除:

Return BMI
Return reading