用作参数的变量不能改变其值? vb.net

时间:2017-05-18 00:45:07

标签: vb.net

我在VB.NET中的任务之一是在vb.net中创建一个程序,该程序从用户那里获取15个输入的测试分数,计算平均值,然后返回相应的字母等级。代码如下。

Module Module1
    Public Test1 As Decimal = 0
    Public Test2 As Decimal = 0
    Public Test3 As Decimal = 0
    Public Test4 As Decimal = 0
    Public Test5 As Decimal = 0
    Public Test6 As Decimal = 0
    Public Test7 As Decimal = 0
    Public Test8 As Decimal = 0
    Public Test9 As Decimal = 0
    Public Test10 As Decimal = 0
    Public Test11 As Decimal = 0
    Public Test12 As Decimal = 0
    Public Test13 As Decimal = 0
    Public Test14 As Decimal = 0
    Public Test15 As Decimal = 0
    Public counter As Integer = 1
    Public letterGrade As Char
    Sub Main()
        Console.WriteLine("This program will take 15 inputted test scores, 
        and then it will return an average and letter grade")
        question(Test1)
        question(Test2)
        question(Test3)
        question(Test4)
        question(Test5)
        question(Test6)
        question(Test7)
        question(Test8)
        question(Test9)
        question(Test10)
        question(Test11)
        question(Test12)
        question(Test13)
        question(Test14)
        question(Test15)
        Dim av As Decimal
        av = (Test1 + Test2 + Test3 + Test4 + Test5 + Test6 + Test7 + Test8 
        + Test9 + Test10 + Test11 + Test12 + Test13 + Test14 + Test15) / 15
        If av >= 90 Then
            letterGrade = "A"
        ElseIf 80 <= av < 90 Then
            letterGrade = "B"
        ElseIf 70 <= av < 80 Then
            letterGrade = "C"
        ElseIf 60 <= av < 70 Then
            letterGrade = "D"
        Else
            letterGrade = "F"
        End If
        MsgBox("You average is" + Str(av) + "%. You got a " + letterGrade + 
        "!")
    End Sub

    Sub question(ByVal score2 As Decimal)
        Console.WriteLine("")
        Console.WriteLine("Enter test score number" + Str(counter))
        input(score2)
        counter += 1
    End Sub

    Sub input(ByVal score As Decimal)
        Try
            score = Console.ReadLine()
            If score < 0 Then
                Throw New Exception()
            End If
        Catch ex As Exception
            Console.WriteLine("You entered an invalid input (number was too 
            large, was a negative, or was not a number)")
            question(score)
        End Try
    End Sub
End Module

程序首先创建15个变量。然后在主程序中,它运行问题功能(有一个参数),要求用户输入分数。最后,它运行输入函数(具有参数),该函数记录用户输入并将其存储在参数中。如果捕获到异常,则输入返回到问题函数并再次询问相同的问题。它执行此操作,直到用户输入有效输入,然后转到下一个测试分数。获得15个输入后,将它们全部平均并找到字母等级。我把我最初创建的变量作为参数,因此它们应该将它们的值更改为用户输入的任何值。悬停,当我运行程序时,它返回平均值0和字母等级B,无论我输入什么数字。我告诉程序在它们应该改变之后打印变量,并且它为所有变量打印0,这是我为它们设置的初始值。为什么会这样?可以将变量用作参数并将其值更改为函数中的用户输入吗?

1 个答案:

答案 0 :(得分:2)

您可以使用函数中使用的变量并保留其值,但为了做到这一点,您必须使用Reference传递。

在您使用的代码中,只需将其更改为ByRef。

Sub question(ByRef score2 As Decimal)

Sub input(ByRef score As Decimal)

与&#39; ByVal&#39;一起使用的参数基本上将它的值复制到另一个变量中,并将其赋予函数,以满足其中需要完成的任何操作。

当您将其设置为Ref(ByRef)时,您实际上是将指针传递给变量本身并直接访问它,而不是复制。

查看this link