vb怎样编程序 2+22+222+2222+22222的和
3个回答
展开全部
如果只算到22222那么就这么简单
Private Sub Command1_Click()
Print 2+22+222+2222+2222
End Sub
如果像自定义算到多少个2那么就用这个
Private Sub Command1_Click()
Dim s As Double, n As Integer, sum As Double
n = InputBox("你想算到多少位?")
s = 2: sum = s
For n = 1 To n
s = 2 * 10 ^ n + s
sum = sum + s
Next
Print “2+22+222+2222+....=" & sum
End Sub
Private Sub Command1_Click()
Print 2+22+222+2222+2222
End Sub
如果像自定义算到多少个2那么就用这个
Private Sub Command1_Click()
Dim s As Double, n As Integer, sum As Double
n = InputBox("你想算到多少位?")
s = 2: sum = s
For n = 1 To n
s = 2 * 10 ^ n + s
sum = sum + s
Next
Print “2+22+222+2222+....=" & sum
End Sub
展开全部
Dim ret As Long
Dim a As Integer
Dim b As Integer
ret = 0
a = 2
b = 0
For i = 0 To 4
For j = 0 To i
b = b + a * 10 ^ j
Next j
ret = ret + b
b = 0
Next i
ret就是所要的值
你可以根据i的值来控制要加多少个2,但一定要控制结果不要溢出了!
Dim a As Integer
Dim b As Integer
ret = 0
a = 2
b = 0
For i = 0 To 4
For j = 0 To i
b = b + a * 10 ^ j
Next j
ret = ret + b
b = 0
Next i
ret就是所要的值
你可以根据i的值来控制要加多少个2,但一定要控制结果不要溢出了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
怎么好多人问这个问题,如果有很大个2,把循环量i改一下就行了:
Private Sub Command1_Click()
Dim i As Integer, k As String, j As Long
For i = 1 To 5
k = String(i, "2")
j = j + k
Print j, k
Next
Print j
End Sub
Private Sub Command1_Click()
Dim i As Integer, k As String, j As Long
For i = 1 To 5
k = String(i, "2")
j = j + k
Print j, k
Next
Print j
End Sub
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询