当前位置:文档之家› VB程序设计例题程序改错程序填空程序设计

VB程序设计例题程序改错程序填空程序设计

'以下程序段用于输出杨辉三角:结果样式如图1'Private Sub Form_Click()Const n = 10Dim arr(n, n) As IntegerFor I = 1 To narr(I, I) = 1'**********SPACE**********【arr(I, 1) = 1】Next IFor I = 3 To nFor j = 2 To I - 1'**********SPACE**********arr(i, j) =arr(i-1,j-1)+arr(i-1,j)Next jNext IFor I = 1 To nFor j = 1 To I'**********SPACE**********【print arr(I,j)】Next jPrintNext IEnd SubOption Explicit'------------------------------------------------'【程序改错】'------------------------------------------------'题目:用自定义函数的方法求sum(x),求当-1≤x≤1时,' sum(x)=x/2!+x^2/3!+x^3/4!+……+x^n/(n+1)!,' 当x〉1或x〈-1时,函数值为0。

当n〈=0时,输入数' 据错误。

X、N都是由用户输入。

'------------------------------------------------'注意:不可增加或删除程序行,也不可以更改程序结构'------------------------------------------------Private Sub Command1_Click()Dim s As SingleDim n As Integer, x As Single, k As Integern = Val(InputBox("Please input a integer value:"))x = Val(InputBox("Please input a single value:"))If n <= 0 Thenk = MsgBox("数据输入错误!", vbRetryCancel + vbExclamation, "数据输入")Exit SubEnd Ifs = Sum(x, n)Print sEnd SubFunction Sum(x As Single, n As Integer)Dim i As Integer, ss As Longss = 1Sum = 0If x > 1 Or x < -1 Then'**********FOUND**********Exit Do 改为Exit FunctionElse'**********FOUND**********For i = 2 To n 改为For i = 2 To n+1ss = ss * i'**********FOUND**********Sum = x ^ (i - 1) / ss 改为Sum =sum+ x ^ (i - 1) / ssNext iEnd IfEnd FunctionOption Explicit'------------------------------------------------'【程序改错】'------------------------------------------------'题目:编程将一个数从已经有序(设从小到大排序)的数组' 中删除,使数组还继续保持有序而且其余元素按照下' 标连续存放。

'------------------------------------------------'注意:不可增加或删除程序行,也不可以更改程序结构'------------------------------------------------Private Sub Form_Click()Dim A() As Integer, N As Integer, Flag As IntegerDim i As Integer, X As Integer, P As IntegerFlag = 0N = InputBox("输入元素个数")'**********FOUND**********Dim A(N) 改为ReDim A(N)For i = 1 To NA(i) = InputBox("输入数组的第" & i & "个元素") Next iPrint "删除某元素前的数组"For i = 1 To NPrint A(i);Next iStart:X = InputBox("输入要删除的元素:")For i = 1 To NIf X = A(i) Then P = i: Flag = 1Next iIf Flag = 0 Then MsgBox ("没有此元素"): GoTo StartFor i = P To N - 1'**********FOUND**********A(i + 1) = A(i) 改为A(i) = A(i+1)Next i'**********FOUND**********N = N + 1 改为N = N- 1Print "删除元素后的数组:"For i = 1 To NPrint A(i);Next iEnd SubPrivate Sub Form_Click()'-------------------------------------------------'【程序设计】'-------------------------------------------------'题目:(事件)单击窗体。

' (响应)输出一个如图所示的由数字组成的倒三角形' 请将结果存入变量S中(要求利用msgbox函数' 显示结果,使用for...next语句完成程序)'-------------------------------------------------'注意:请在指定的事件内编写代码。

'-------------------------------------------------Dim s As String'***************** Program **************Dim i%, j%, w$w = ""For i = 10 To 1 Step -1For j = 0 To i - 1w = w & CStr(j)Nextw = w & vbCrLfNextMsgBox ws = w'*************** End *************************Call YZJ(s)End SubPrivate Sub YZJ(i As String)Dim OUT As IntegerOUT = FreeFileOpen App.Path & "\out.txt" For Output As #OUTPrint #OUT, iClose #OUTEnd SubPrivate Sub Form_Click()'-------------------------------------------------'【程序设计】'-------------------------------------------------'题目:(事件)单击窗体。

' (响应)求100到500之间奇数的和,将结果输出' 在窗体上并存入变量SUM中。

' 使用do...loop until语句完成程序'-------------------------------------------------'注意:请在指定的事件内编写代码。

'-------------------------------------------------Dim sum As Long'***************** Program **************'*************** End ************************* Call YZJ(sum)End SubPrivate Sub YZJ(i As Long)Dim OUT As IntegerOUT = FreeFileOpen App.Path & "\out.txt" For Output As #OUTPrint #OUT, iClose #OUTEnd SubOption Explicit'-------------------------------------------------------'【程序填空】'-------------------------------------------------------'题目:下面的程序用于求三个数中最大数'请在【?】处填入正确的内容'-------------------------------------------------------'Private Sub Command1_Click()Dim a, b, c, max, min As Integera = Val(InputBox("请输入第一个数:"))b = Val(InputBox(" 请输入第二个数:"))c = Val(InputBox("请输入第三个数:"))'**********SPACE**********【?】= amin = a'**********SPACE**********If b > max Then 【?】If b < min Then min = b'**********SPACE**********If 【?】Then max = cIf c < min Then min = cPrint "最大数为:"; maxPrint "最小数为:"; minEnd Sub'下面过程max()用于求3个数中最大值,利用这个过程求5个数中最大值。

相关主题