设a、b都是自然数,为求a除以b的余数,某人编写了以下函数: Function fun(a As Integer,b As Integer) While a>b     a=a-b Wend fun=a End Function 在调试时发现函数是错

admin2020-06-30  21

问题 设a、b都是自然数,为求a除以b的余数,某人编写了以下函数:
Function fun(a As Integer,b As Integer)
While a>b
    a=a-b
Wend
fun=a
End Function
在调试时发现函数是错误的。为使函数能产生正确的返回值,应做的修改是(  )。

选项 A、把a=a-b改为a=b-a
B、把 a=a-b 改为a=a\b
C、把While a>b 改为While aD、把While a>b 改为While a>=b

答案D

解析 程序中没有考虑余数为0的情况,即,条件中应该有a=b,则a=a-b,这样才能正确得到余数0。因此程序中应该修改While的条件a>b为a>=b。
转载请注明原文地址:https://kaotiyun.com/show/VIHp777K
0

相关试题推荐
最新回复(0)