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\/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里面那几个表示循环的函数怎么用
1、运行For语句和Do语句的循环语句的功能示范.frm(注意步长为负时,如-1时For语句的运行情况讨论),掌握它们的功能;2、例2:求S=1+1\/2+1\/3+…+1\/n的循环结构程序如下:(演示:例2累加求和程序.FRM,再一次说明为什么要用第二种Do循环语句)S = 0 n = Val(Text1.Text) (因为条件表...

用VB写设计程序代码,求:s=1+(1+2)+(1+2+3)+??+(1+2+3+??+n)的值。
Function sumD(n As Integer) As Single s = 1 i = 1 Do i = i + 1 s = s + 1 \/ i Loop Until i >= n sumD = s End Function Function sumF(n As Integer) As Single s = 0 For i = 1 To n s = s + 1 \/ i Next i sumF = s End Function Private Sub Form_...

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

高一信息题 :编写程序计算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 ...

VB编程 已知N=30,求级数sum=1+1\/2+1\/3+1\/4···+1\/n的和
依据你的描述计算结果如图 代码如下 Private Sub Command1_Click()Sum = 0 For n = 1 To 30 If 1 \/ n <= 0.0000001 Then Exit For Sum = Sum + 1 \/ n Next n Print "计算到第" & n - 1 & "项"Print "和值为:" & Sum End Sub ...

java语言程式设计 输入一个正整数N,输出表达式1+1\/2+1\/3+.+1\/N的值
java语言程式设计 输入一个正整数N,输出表达式1+1\/2+1\/3+...+1\/N的值 package test;import java.util.Scanner;public class Express { public static void main(String[] args) { System.out.println("请输入n的值"); Scanner scan=new Scanner(System.in); int n=scan.nextIn...

相似回答