此代码是该任务的正确代码吗? [英] Is this code the right code for this task?

查看:63
本文介绍了此代码是该任务的正确代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Create a function called swapped that will take two integers(a,b) called byref and return a Boolean that is true if the numbers have been swapped. Within the routine compare the numbers and if b>a swap them using a local variable temp, then return the value true else return the value 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

推荐答案

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

Here is a fixed example. You should have the parameters declared as integers instead of boolean. The function itself should be declared as a boolean

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

这篇关于此代码是该任务的正确代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆