求输入一个正数k,求其各位数字之和!~!~VB大侠来!
发布网友
发布时间:2024-10-05 16:30
我来回答
共4个回答
热心网友
时间:2024-10-09 03:22
我来一个 最符合题意 最不会出错的!
比楼上的功能多的:
1.能判断是否是数字;
2.能判断是否是正数;
3.能支持小数点的忽略计算,计算方式:忽略小数点,其他一样;
4.只要输入的数据[位数]小于1.79769313486232E308位,就不会溢出(汗 那么长的数字就算输入也要输入半天…………)注意 是位数哦 不是大小!
以下是代码
Dim k As String, p() As Integer, i As Integer, sum As Double
goRe: k = InputBox("请输入一个正数", "输入")
If IsNumeric(k) = False Then MsgBox "请输入数字!": GoTo goRe
If CDbl(k) <= 0 Then MsgBox "请输入正数!": GoTo goRe
For i = 1 To Len(k)
sum = sum + IIf(IsNumeric(Mid(k, i, 1)) = True, Val(Mid(k, i, 1)), 0)
Next
MsgBox "各个位数数字和为: " & Trim(Val(sum))
热心网友
时间:2024-10-09 03:22
'只处理正整数
Dim S As Long
k = InputBox("请输入一个正数", , "0")
tmp = CStr(Val(k))
L = Len(tmp)
For i = 1 To L
S = S + Val(Mid(tmp, i, 1))
Next i
MsgBox S
热心网友
时间:2024-10-09 03:24
我保证效率高一点
Private function test(ByVal n as Integer)
Dim sum As Integer
Dim y As Integer
sum=0
while n>0
y = (n mod 10)
n = int(n / 10)
sum = sum + y
Wend
test = sum
End function
热心网友
时间:2024-10-09 03:19
这很简单,看下面的
Private Sub Form_Load()
Dim A As Long, k As String
k = InputBox("输入一个正整数")
For i = 1 To Len(k)
A = A + Mid(Val(k), i, 1)
Next i
MsgBox A
End Sub