此代码是该任务的正确代码吗?

编程入门 行业动态 更新时间:2024-10-24 03:24:06
本文介绍了此代码是该任务的正确代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

创建一个名为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

更多推荐

此代码是该任务的正确代码吗?

本文发布于:2023-11-08 07:01:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568668.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   正确

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!