VB程序设计教程题,设s=1+1/2+1/3……+1/n,求与8最接近的s的值及与之对应的n值

我设计的只能是单方面的从8的左端或右端接近8,而不能让它们两个进行比较后直接把最终结果从窗体上显示出来。提前谢谢了

Private Sub Form_Click()
Dim I As Integer
Dim S As Single
Form1.Cls
S = 0
While S < 8
I = I + 1
S = S + (1 / I)
Wend
Print I; S; Abs(8 - S)
I = I - 1
S = S - (1 / I)
Print I; S; Abs(8 - S)
End Sub
根据上面的代码,相信你能很快看出结果。
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-04-28
Sub test1()

FindN (8)

End Sub

Function FindN(K)
Dim n
Dim a1
Dim s
Dim s1
n = 0
Do While s < K
s1 = s
n1 = n
n = n + 1
a1 = 1 / n
s = s + a1
Loop
Print s1
Print s
Print "更接近于" & K & "的是";
If 8 - s1 > s - 8 Then
Print s
Print "同时n=" & n
Else
Print s1
Print "同时n=" & n1
End If
End Function

VB程序设计教程题,设s=1+1\/2+1\/3……+1\/n,求与8最接近的s的值及与...
Private Sub Form_Click()Dim I As Integer Dim S As Single Form1.Cls S = 0 While S < 8 I = I + 1 S = S + (1 \/ I)Wend Print I; S; Abs(8 - S)I = I - 1 S = S - (1 \/ I)Print I; S; Abs(8 - S)End Sub 根据上面的代码,相信你能很快看出结果。

用VB编写函数过程,计算s=1+1\/2+1\/3+…+1\/100。运行程序时,单击窗体...
private sub Form_Click()dim i as long dim s as currency s=1 for i=2 to 100 s=s+(1\/i)next i msgbox s end sub '这个程序会弹出精确到4位小数的结果,如果需要更高精度的,可以试着把currency换成double。

在vb程序中,计算1+1\/2+1\/3+……+1\/n的程序
VB.NET代码:dim i,n as Longdim s as Doublen=Console.ReadLine()s=0.0for i=n to 1 step -1s=s+1.0\/inext iConsole.WriteLine("s={0:e}",s)Console.WriteLine("s={0:g}",s)

VB编程求sum=1+1\/2+1\/3+...+1\/n
Private Sub Form_Click()Dim i As Integer Dim sum As Double Dim t As Double sum = 0 i = 1 t = 1 \/ i Do While t > 0.0001 sum = sum + t i = i + 1 t = 1 \/ i Loop Debug.Print "i="; i; "sum="; sum End Sub ...

vb里面那几个表示循环的函数怎么用
S = S + 1 \/ k 循环求S=1+1\/2+1\/3+…+1\/n Next k Text2.Text = S 3、两种循环结构语句的格式:(1) For语句的一般格式: P79 For 循环变量=初值 To 终值 Step 步长 程序段 Next循环变量 (当循环变量没超出终值时,执行程序段)(2) Do语句的常用格式: P80...

VB编程计算S=1+1\/2+1\/3+1\/4+...+1\/n 要求最后一项不小于0.0001
Private Sub Command1_Click()Dim s As Single n = Val(InputBox("输入 一个整数"))If 1 \/ n < 0.0001 Then MsgBox "1\/n要不小于0.0001"Exit Sub End If For i = 1 To n s = s + 1 \/ i Next Print s End Sub

各位高手求VB代码两题 1,S=1+1\/2+1\/3+1\/4...1\/n当某项<=10的-7次方...
第一题:Dim s As Double, p As Double Dim n As Long s = 0: n = 1 Do While n <= 30 p = 1 \/ n s = s + p n = n + 1 Loop Print s 第2题:设label1的AutoSize为真 Dim i As Integer, x As Integer Randomize Label1.Caption = ""For i = 1 To 20 x = Rnd...

高一信息题 :编写程序计算s=1\/1+1\/2+1\/3+……1\/100的和。输出结果 流程...
如图

如何编写vb过程 s=1+1\/2+1\/3+……+1\/100
dim a,b as double b=0 a=1 for a = 1 to 100 b = b + (1\/a)next msgbox "结果为" & b 思路是,用一个变量来储存得数,用for把a(表示计算到哪里)加上去

...用两种不同的方法求1+1\/1!+1\/2!+1\/3!+…+1\/n!
s, i End Sub Private Sub Command2_Click()Dim s, i, k s = 1 Do i = i + 1 k = 1 \/ fc(i)If k < 10 ^ (-6) Then Exit Do s = s + k Loop Print s, i - 1 End Sub Function fc(n)Dim i fc = 1 For i = 1 To n fc = fc * i Next End Function ...

相似回答