编写VB程序,求Sn=a+aa+aaa+…(n个a)的值,其中n是表示a的个数,a是【1,9】之间的随机正整数

2024-12-23 10:08:09
推荐回答(5个)
回答1:

Private Sub Command1_Click()
Dim a%, n%, i%, Sn$
n = InputBox(请输入n的值)
Randomize
a = Int(9 * Rnd + 1)
Print Tab(30); "n=" & n, " a = " & a; ""
Temp = a
Picture1.Print "Sn="; a;
For i = 1 To n - 1
Temp = Temp * 10 + a
Picture1.Print "+"; Temp;
Next
Print
End Sub
(这样比较合适吧)

回答2:

dim Sn,a,n,i,m
m=0
Sn=0
a=int(rnd()*10)
n=text1.text
for i=1 to n
m=10^(i-1) *a+m
Sn=Sn+m
next i
(应该就是这样了,有点久没用VB,有点忘了)

回答3:

Private Sub Command1_Click()
Dim n%, a%, Sn&, Tmp$, i%
n = InputBox("Input N= ")
Randomize
a = Int(Rnd * 10) + 1
Print "n=" & n, " a = " & a; ""
For i = 1 To n
Tmp = String(i, CStr(a))
Print Tmp
Sn = Sn + CInt(Tmp)
Next i
Print "Sn=" & Sn
End Sub

回答4:

Private Sub Form_Click()
Dim sn&
a = Int(Rnd * (9 - 1 + 1) + 1)
n = Int(Rnd * (10 - 5 + 1) + 5)
Randomize
t = a
sn = t
For i = 1 To n - 1
t = t * 10 + a
sn = sn + t
Next i
Print "a="; a; ",n="; n; ",sn="; sn
End Sub

回答5:

Dim a%, n%, i%, Sn%, Temp%
n = InputBox("Input N= ")
Randomize()
a = Int(9 * Rnd() + 1)
Temp = a
Sn = Temp
For i = 1 To n - 1
Temp = Temp * 10 + a
Sn = Sn + Temp
Next
Console.WriteLine(a)
Console.WriteLine(Sn)
运行了是OK的