编写程序计算s=1+3+5+...+n求s=100得值,请画出流程图,并写出相应的VB语句代码
编写程序计算s=1+3+5+...+n求s=100得值,请画出流程图,并写出相应的VB语句代码
Option Explicit
Private Sub Command1_Click()
Dim s As Single, n As Long, st As String
n = 1
s = 0
Do
DoEvents
s = s + n
st = st & n
If s = 100 Then
MsgBox "计算s=1+3+5+...+n,s=100得值结果为:" & Chr(13) & Chr(10) & st & "= " & s & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "n的值为: " & n
Exit Do
End If
st = st & " + "
n = n + 2
Loop
End Sub好多看不懂= =Option ExplicitPrivate Sub Command1_Click()Dim s As Single, n As Long, st As Stringn = 1 ‘第一个奇数s = 0 ‘初始值为0Do ’循环开始DoEvents ‘转让控制权,注:为了防止死循环。s = s + n ‘ 循环时,相当于s=1+3+5+...+nst = st & n '为了能知道循环中N出现的值。If s = 100 ThenMsgBox "计算s=1+3+5+...+n,s=100得值结果为:" & Chr(13) & Chr(10) & st & "= " & s & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "n的值为: " & nExit Do‘发现S=100,显示结果,退出循环End Ifst = st & " + " n = n + 2 ’未发现S=100,n的值变为下一个奇数。LoopEnd Sub