怎样用VB编写1+2+3+4+5+……+n的程序,n的值可以输入。最好发截图

2024-12-17 05:15:09
推荐回答(3个)
回答1:

关键代码如下,按钮1点击

Private Sub Command1_Click()
Dim n As Integer, sum As Integer, i As Integer
n = CInt(Text1.Text)
For i = 1 To n
sum = sum + i
Next i
Text2.Text = sum
End Sub

截图

回答2:

按纽click事件dim i as integerdim sum as integerdim n as integern = val(text1.value)sum = 0for i = 1 to nsum = sum + inexttext2.value = str(sum)大体上这样,好久没用VB了,转换函数也忘了

回答3:

Private Sub Command1_Click()
Dim a As Integer
a = Val(Text1.Text)
Sum = 0
For k = 1 To a
Sum = Sum + k '累加
Next k
Text2.Text = Sum
End Sub