假设用户输入一个像“3452”这样的数字我想要一个函数逐个添加这个数字,直到得到一位数字,如下所述:
第一个方面是:
3 + 4 = 7 - > 7 + 5 = 12 - >(如果位数超过1)1 + 2 = 3 - > 3 + 2 = 5
第二个方面是:
3 + 4 = 7 - > 7 + 5 = 12 - > 12 + 2 = 14 - > 1 + 4 = 5
我写了这个:
Function Sum(number)
Dim i, intTemp
intTemp = 0
For i=1 to Len(number)
intTemp = intTemp + CInt(Mid(number, i, 1))
Next
Sum = intTemp
End Function
但是这个函数返回2位数而不是一位数...请指导我
编辑:我在if语句中放置了一个Do while循环,if语句检查intTemp数字的长度如下:
if Len(intTemp)>1 then
Do While Len(intTemp) = 1
some code like first section
Loop
end if
但它不起作用! :(
答案 0 :(得分:2)
使用你的求和数字逻辑对我来说相当简单。 intTemp有两位数后我需要递归(我将名称改为 DigitSum ):
Function DigitSum(number)
Dim i, intTemp, sNumber
intTemp = 0
sNumber = CStr(number)
For i = 1 To Len(sNumber)
intTemp = intTemp + CInt(Mid(sNumber, i, 1))
If Len(CStr(intTemp)) > 1 Then intTemp = DigitSum(intTemp)
Next
DigitSum = intTemp
End Function
答案 1 :(得分:0)
如果你做了这样的事情怎么样?它假设您传入的数字超过一个数字(大于9),并将继续循环,直到您得到一位数的结果。
Function Sum(number)
Dim i, intTemp, workingNum
workingNum = number
Do
intTemp = 0
For i=1 to Len(workingNum)
intTemp = intTemp + CInt(Mid(number, i, 1))
Next
workingNum = intTemp
Loop until workingNum < 9
Sum = intTemp
End Function
答案 2 :(得分:0)
由于我不能按照您的规范制作头或尾,我创建了一个“让它易于实验”的脚本来播放两个函数,为您的(单个!)样本提供预期的结果:
Option Explicit
Const cbDebug = False
WScript.Quit demoMain()
Function demoMain()
Dim aTests : aTests = Array( _
Array("3452", 5, 5) _
, Array("0", 0, 0) _
, Array("", 0, 0) _
, Array("99", 9, 9) _
, Array("999999", 9, 9) _
, Array("991999", 1, 1) _
)
Dim aTest
For Each aTest In aTests
Dim sInp : sInp = aTest(0)
WScript.Echo "--------", qq(sInp)
Dim nExp : nExp = aTest(1)
Dim nAct : nAct = fancySum01(sInp)
WScript.Echo " Asp 1:", nExp, nAct, CStr(nExp = nAct)
nExp = aTest(2)
nAct = fancySum02(sInp)
WScript.Echo " Asp 2:", nExp, nAct, CStr(nExp = nAct)
Next
demoMain = 0
End Function
'' "3452" 3+4=7 -> 7+5= 12 ->( if number of digits exceed from 1 ) 1+2= 3 -> 3+2= 5
Function fancySum01(sInp)
fancySum01 = 0
Dim p
For p = 1 To Len(sInp)
fancySum01 = fancySum01 + CByte(Mid(sInp, p, 1))
If fancySum01 > 9 Then fancySum01 = (fancySum01 \ 10) + (fancySum01 Mod 10)
If cbDebug Then WScript.Echo " *", p, fancySum01
Next
End Function
'' "3452" 3+4= 7 -> 7+5= 12 -> 12+2= 14 -> 1+4= 5
Function fancySum02(sInp)
fancySum02 = 0
Dim p
For p = 1 To Len(sInp)
fancySum02 = fancySum02 + CByte(Mid(sInp, p, 1))
If cbDebug Then WScript.Echo " *", p, fancySum02
Next
Dim r : r = CStr(fancySum02)
If 1 < Len(r) Then fancySum02 = fancySum02(r)
End Function
Function qq(s) : qq = """" & s & """" : End Function
示例输出(cbDebug = False)
cscript 20845743.vbs
-------- "3452"
Asp 1: 5 5 True
Asp 2: 5 5 True
-------- "0"
Asp 1: 0 0 True
Asp 2: 0 0 True
-------- ""
Asp 1: 0 0 True
Asp 2: 0 0 True
-------- "99"
Asp 1: 9 9 True
Asp 2: 9 9 True
-------- "999999"
Asp 1: 9 9 True
Asp 2: 9 9 True
-------- "991999"
Asp 1: 1 1 True
Asp 2: 1 1 True
我真的想暗示你试图解决的现实问题。
答案 3 :(得分:0)
我在这里声明变量&amp;分配值
Dim str
str = InputBox("Enter the value")
这里我用临时值来打印结果的@结尾
temp = str
otp = 0
sum = 0
while(Len(str)>1)
For i = 1 to Len(str)
sum = sum+cint(mid(str,i,1))
Next
If otp = 0 Then
otp = sum
End If
str = cstr(sum)
sum = 0
wend
MsgBox "Total sum = "& otp
MsgBox "Single digit of "&temp+" is :"&str