【问题标题】:Is this code the right code for this task?此代码是否是此任务的正确代码?
【发布时间】:2020-02-25 01:45:40
【问题描述】:

创建一个名为 swapped 的函数,它将接受两个称为 byref 的整数 (a,b) 并返回一个布尔值,如果数字已被交换,则返回 true。在例程中比较数字,如果 b>a 使用局部变量 temp 交换它们,则返回值 true,否则返回值 false。

Function swapped(ByRef a As Boolean, ByRef b As Boolean)
    a = True
    b = True
    If b > a Then
        temp = b
        b = a
        a = temp
        a = True
        b = True
    Else
        a = False
        b = False
    End If

【问题讨论】:

  • 感谢您的尝试,但“取两个整数” - 您看到这里的第一个问题了吗?
  • (ByRef a As Integer, ByRef b As Integer) 你认为我应该把它改成这样吗?
  • 我建议您与您的导师/老师谈谈您的困惑。他们会知道他们教给你什么材料,并且更有能力解决这个问题。

标签: vb.net function


【解决方案1】:

首先让我们看看你的代码。

'The requirements called for 2 Integers, not Booleans.
'A Function requires a datatype, in this can a Boolean
Private Function swapped(ByRef a As Boolean, ByRef b As Boolean)
    'Assigning values here will overwrite the passed in values
    a = True
    b = True
    'What would it mean for True > True ??
    If b > a Then
        'temp isn't declared so this will not compile
        temp = b
        'here I think you are trying to do the swap
        'but all the values are True
        b = a
        a = temp
        a = True
        b = True
    Else
        a = False
        b = False

    End If
    'Function need a return value to match the datatype of the function
    'It is preceded by Return
End Function

如果您使用的是 Visual Studio(免费下载),则会为您突出显示其中的几个错误。

现在,让我们看一些可以完成任务的代码。

'Through comments you have already figured out that a and b are Integers
'In addition, Functions always need a return type. In this case a Boolean
    Function swapped(ByRef a As Integer, ByRef b As Integer) As Boolean
    'The values of a and b are passed to the function.
    'Keep the passed in value of a in a new variable
    If b > a Then
        Dim aPassedInValue = a
        'assign the value of b to a
        a = b
        'now assign the original value of a to b
        'we can't use a because we just changed the value of a in the above line
        b = aPassedInValue
        'There have been no errors so we can return True
        Return True
    Else 'We didn't swap them
        Return False
    End If
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim c As Integer = 7
    Dim d As Integer = 20
    MessageBox.Show($"c = {c}, d = {d}", "Before Swap")
    Dim ReturnValue = swapped(c, d)
    MessageBox.Show($"The return value of the swapped Function is {ReturnValue}")
    MessageBox.Show($"c = {c}, d = {d}", "After Swap")
End Sub

我认为这个练习的重点是演示 ByRef。您已更改函数中的值,并且新值反映在 Button 中的变量中。单击 Sub。

【讨论】:

    【解决方案2】:

    这是一个固定的例子。您应该将参数声明为整数而不是布尔值。函数本身应声明为布尔值

    Function swapped(ByRef a As Integer, ByRef b As Integer) As Boolean
    
        If b > a Then
           'Declare the temp variable
            Dim temp As Integer = b
    
           'Change b to a
            b = a
           'Set 'a' equal to the temp variable from the original b
            a = temp
    
            Return True
    
        Else
            Return False
    
        End If
    End Function
    
    Public Sub test()
        Dim intA As Integer
        Dim intB As Integer
        intA = 1
        intB = 2
        'Test b>a
        'Should return True
        Dim check As Boolean
        check = swapped(intA, intB)
    
        'Test Not(b>a)
        'Should return False
        Dim checkfalse As Boolean
        checkfalse = swapped(2, 2)
    End Sub
    

    【讨论】:

    • 这里使用预 .NET 语法 functionName = resultValue 代替 .NET 语法 Return resultValue 从函数返回值。是的,它有效,但它通常不会再看到了。
    • a 是整数,b 是整数。他们不能接受布尔值。
    • @djv,是的,出于某种原因,我认为他们正在使用 VBA 来编写他们的函数。所以,我只是在那里做了。它带我回到过去。
    • @Mary,这是描述中的要求。 "创建一个名为 swapped 的函数,它将接受两个整数 (a,b)...." 如果应该返回一个 boolean 值,则该函数本身。
    • 糟糕,你说的很对。那是个错误。我会编辑它。感谢您指出了这一点。我会编辑它。另外,在variant 上,我不知道为什么我认为他们在使用 VBA,但这就是我这样做的原因。
    【解决方案3】:

    现有的两个答案都可以解决问题。这是一个没有临时变量的变量

    Private Function swapped(ByRef a As Integer, ByRef b As Integer) As Boolean
        If b > a Then
            a = a + b
            b = a - b
            a = a - b
            Return True
        End If
        Return False
    End Function
    
    Sub Main()
        Dim a = 10
        Dim b = 11
        Console.WriteLine($"a: {a}, b: {b}")
        Console.WriteLine($"values were{If(swapped(a, b), " ", " not ")}swapped.")
        Console.WriteLine($"a: {a}, b: {b}")
        Console.ReadLine()
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      相关资源
      最近更新 更多