VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • vb.net教程之VB编程实例求随机10个整数的最大值、

1. 求随机10个整数的最大值、最小值、平均值以及和;

Dim a(1 To 10)
Private Sub Command1_Click()
Randomize
Picture1.Print "产生的随机数为:"
For i = 1 To 10
a(i) = Int(Rnd * 99 + 1)
Picture1.Print a(i);
Next i
Picture1.Print
End Sub

Private Sub Command2_Click()
Dim max, min, ave
max = a(1)
min = a(1)
ave = 0.1 * a(1)
For i = 2 To 10
If a(i) > max Then max = a(i)
If a(i) < min Then min = a(i)
ave = ave + 0.1 * a(i)
Next i
Picture1.Print "最大数为:"; max
Picture1.Print "最小数为:"; min
Picture1.Print "平均数为:"; ave

End Sub

2.求水仙花数
Private Sub Form_Click()
Dim a, b, c As Integer 'a(个)b(十)c(百)
For a = 0 To 9
For b = 0 To 9
For c = 1 To 9
If a ^ 3 + b ^ 3 + c ^ 3 = a + 10 * b + 100 * c Then
MsgBox 100 * c + 10 * b + a
End If
Next c
Next b
Next a
End Sub

3.百元买百鸡问题;
Option Explicit
Const a = 5, b = 3, c = 1
Private Sub Form_Activate()
Dim i As Integer, j As Integer, k As Integer, n As Integer
For i = 1 To 100
For j = 1 To 100
For k = 1 To 100
If i * a + j * b + k * c = 100 Then
n = n + 1
List1.AddItem " 公鸡:" & i & " 母鸡:" & j & " 小鸡:" & k
End If
Next: Next: Next
MsgBox "共有组合:" & n
End Sub

求1000以内的所有完数
一个按钮里调用的过程
Private Sub Command1_Click()
OutputWanNumber 1000
End Sub

4.'求完数的过程
Private Sub Form_Click()
Dim i, j As Integer
For i = 4 To 100
temp = 1
For j = 2 To i / 2
If i Mod j = 0 Then
temp = temp + j
End If
Next j
If temp = i Then
Print i
End If
Next i
End Sub

5.求各位数字之和
input a:'输入任意数
do
b=a mod 10:'取a的末位数
sum=sum+b:'求和
a=a \ 10:'去掉末位数
loop until a=0
print "sum=";sum:'输出
end
(dim n as string
input n
for i = 1 to len(n)
sum=sum+val(mid$(n,i,1))
next i
print sum)
6.求最小公倍数
Private Sub Form_Load()
Form1.AutoRedraw = True

Dim n1%, m1%, m%, n%, r%
n1 = InputBox("输入n1")
m1 = InputBox("输入m1")
If m1 > n1 Then '为了求最小公倍数,增加m,n变量
m = m1: n = n1
Else
m = n1: n = m1
End If
Do
r = m Mod n
If r = 0 Then Exit Do
m = n
n = r
Loop
Print n1; ","; m1; "的最大公约数为"; n
Print "最小公倍数=", m1 * n1 / n
End Sub

7.求逆序数(感觉题目类型太多)

8. 级数有限项求和问题(题目类型太多)

9. 求质因子问题
Private Sub Command1_Click()
Dim N As Integer, I As Integer
N = Val(InputBox("请输入2的整数:"))
I = 2
Do
If N Mod I = 0 Then
Print I;
N = N \ I
Else
I = I 1
End If
Loop While N 1
End Sub

10. 字符统计
Option Base1
Option Explicit
Private Sub Command1_Click()
Dim i as integer,A(26) as integer,n as integer
Dim S as string*1,Strl as string
Strl=Text1
n=Len(Strl)
For i=1 To n
S=Mid(Strl,i,1)
If UCase(S)>=”A” And UCase(S)<=”Z” Then
A(Asc(UCase(S))-64)+1
End If
Next i
For i=1 To 26
List1.Additem Chr(64+i) & “:” & A(i)
Netx i
End Sub
Private Sub Command_Click()
End
End Sub
第二大题
1. 判定素数过程
Function isprime(Num As Long) As Boolean
If Num < 2 Then isprime = False: Exit Function

Dim i As Long

For i = 2 To Sqr(Num)
If (Num Mod i) = 0 Then
isprime = False
Exit Function
End If
Next i

isprime = True
End Function

Private Sub Command1_Click()
Dim i As Long

For i = 1 To 1000
If isprime(i) Then
Print i
End If
Next i

End Sub

2.求最大公约数过程;
Function Max公约数(A As Long, B As Long) '求出两个数的最大公约数
Dim X As Long, Y As Long, K As Long
X = IIf(A >= B, A, B) 'x存入最大值
Y = IIf(A <= B, A, B) 'y 存入最小值
Do '辗转相除法
K = X Mod Y: If K = 0 Then Exit Do
X = Y: Y = K
Loop
Max公约数 = Y
End Function

3.冒泡排序过程
Private Sub Command1_Click()
Dim a(9) As Integer
Dim i As Integer

For i = 0 To 9
a(i) = InputBox("输入整数")
Next
Sort a
For i = 0 To 9
Print a(i)
Next
End Sub

Private Sub Sort(ByRef a() As Integer)
Dim i As Integer
Dim j As Integer
Dim t As Integer

For i = LBound(a) + 1 To UBound(a)
For j = UBound(a) To i Step -1
If a(j - 1) > a(j) Then
t = a(j - 1)
a(j - 1) = a(j)
a(j) = t
End If
Next
Next
End Sub


4顺序查找过程
Private Sub Command1_Click()
Dim i, j, t, a(1 To 10)
Randomize
Print "原数组:"
For i = 1 To 10
a(i) = Rnd * 10
Print "a(" & i & ") =" & a(i) & Space(2),
If i Mod 2 = 0 Then Print
Next i
Print
For i = 1 To 9
For j = i + 1 To 10
If a(j) < a(i) Then
t = a(i)
a(i) = a(j)
a(j) = t
End If
Next j
Next i
Print "从小到大排序后数组:"
For i = 1 To 10
Print "a(" & i & ") =" & a(i) & Space(2),
If i Mod 2 = 0 Then Print
Next i
End Sub

5. 判断回文数过程
建一个Text1,Text2,Command1。
Text1,Text2的Multiline都设置为True。
代码如下。
============
Private Sub Command1_Click()
Text2.Text = ""
Dim a, i As Integer, n, s As String
a = Split(Text1.Text, vbCrLf)
For Each n In a
s = s & n
If n = StrReverse(n) Then s = s & "★"
s = s & vbCrLf
Next
Text2.Text = s
End Sub

6.递归函数求阶乘
递归法求阶乘
Private Sub Command1_Click()
n = InputBox("输入自然数", "输入")
JieCheng n
If n > 1 Then
For j = n To 2 Step -1
Print j & "*";
Next j
End If
Print "1=";
Print JieCheng(n)
End Sub
Private Function JieCheng(ByVal i&) As Long
If i > 1 Then
JieCheng = i * JieCheng(i - 1)
Else
JieCheng = 1
End If
End Function
7.递归函数求Fibonacci数列
Option base1
Option explicit
Private sub command1_click()
Dim n as integer, i as integer
Dim f() as long
n=text1
Redim f(n)
F(1)=1
List1.Additem F(1)
F(2)=1
List1,Additem F(2)
For i=3 To n
F(i)=F(i-2)+F(i-1)
List1.Additem F(i)
Next i
End Sub
Private Sub Command2_Click()
End
End Sub
8.矩阵靠边元素之和

Private Sub Form_Click()
Cls
Randomize
Dim a(5, 5) As Integer
For i = 0 To 5
For j = 0 To 5
a(i, j) = Int(Rnd * 9)
Print a(i, j) & ",";
Sum = Sum + a(i, j)
Next j
Print
Next i
Print "所有元素和为:" & Sum
'////////////
Sum = 0
For i = 0 To 5
Sum = Sum + a(i, 0) + a(i, 5) + a(0, i) + a(5, i)
Next
Sum = Sum - a(0, 0) - a(5, 0) - a(5, 5) - a(0, 5)
Print "所有靠边元素的和:" & Sum
'//////////////////////////////
备用的《Sum = 0
For i = 0 To 5
Sum = Sum + a(i, i) + a(i, 5 - i)
Next
Print "两条对角线元素的和为:" & Sum》
End Sub

9.矩阵不靠边元素之和
利用第8题求出的所有和,减去靠边和即可
10矩阵转置
Private Sub Command1_Click()
Dim a() As Integer
n = InputBox("输入行个数")
m = InputBox("输入列个数")
ReDim a(1 To n, 1 To m)
Randomize
Print "生成矩阵"
For i = 1 To n
For j = 1 To m
a(i, j) = Int(Rnd * 100 + 1)
Print a(i, j);
Next j
Print
Next i
End Sub

Private Sub Command2_Click()
Print "转置"
For i = 1 To n
For j = 1 To m
if i<j then
temp = a(i, j)
a(i, j) = a(j, i)
a(j, i) = temp
end if
Print a(i, j);
Next j
Print
Next i
End Sub
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
第一道:教材第69页,关于素数的那道题!
第二道:练习册61页第6题!
第三道:打印出 y=sin x 的图像!
第四道:打印出 y=x的平方的图像!
第五道:随机产生一个两位整数,将其转化成二进制数,用whilewend语句!
第六道:随机产生一串字母,然后到序排列!
Private Sub Command1_Click()
Dim x(10) As Integer, y(10) As String, max As Integer
For i = 1 To 10
x(i) = Int(Rnd * 26 + 65)
y(i) = Chr(x(i))
Label1.Caption = Label1.Caption & y(i) & " "
Next i
For i = 1 To 10
For j = i + 1 To 10
If x(i) < x(j) Then
max = x(j)
x(j) = x(i)
x(i) = max
End If
Next j
y(i) = Chr(x(i))
Label2.Caption = Label2.Caption & y(i) & " "
Next i
End Sub
第七道:设计一个窗体,主菜单,快捷键O控制,把主菜单标题为选项(&O)“,然后编辑代码如下,代码是本人今天考试写的,仅供参考!
Dim a As Integer

Private Sub mnuoo_Click()
Text1.Text = ""
a = Int(Rnd() * 50 + 30)
Text1.Text = a
End Sub
Private Sub munnn_Click()
Label1.Caption = ""
If a Mod 2 <> 0 Then
Label1.Caption = "奇数"
Else
 If a Mod 2 = 0 Then
 Label1.Caption = "非奇数"
 End If
 End If
 
End Sub

第八道:'题目:下面程序可输出如下图形:
'           *
'          ***
'         *****                    
'        *******
'       *********
'------------------------------------------------
Option Explicit
Private Sub Form_Click()
Dim m As Integer, n As Integer, s As String, i As Integer, j As Integer
n = 4
m = 1
s = "*"
For i = 5 To 1 Step -1
   '**********FOUND**********;
   Print Spc(n);
   For j = 1 To 2 * m - 1
       Print s;
   Next j
   Print
    n = n - 1
  '**********FOUND**********
   m = m + 1
Next i
End Sub
VB重要知识点索引
 
 
VB的基本特点(具有基于对象的可视化设计工具、事件驱动的编程机制等)    P5
对象和类                P20
对象的三要素——属性、事件和方法                       P21
常见的通用属性:                                                  P24
     Name                                                           Caption
     Height     Width    Top    Left 
     Font             FontName(字体,如宋体等)、FontSize(字体的大小)、FontBold(粗体)、FontItalic(斜体)、FontStrikethru(删除线)、FontUnderline(下划线)
     Enabled                                                        Visible
 
窗体      P26      特别的几个属性:MaxButton MinButton  最大、最小化按钮
(属性、事件、方法)                  BorderStyle  控制窗体的边框、按钮、大小调整
                                                     WindowsState   窗体执行时的大小状态
 
标签       P29
文本框    P31      特别的几个属性:MaxLength  文本长度
                                                      MultiLine   使文本显示多行
                                                       ScrollBars   设置滚动条
 
命令按钮   P34                标题Caption中,如果某个字母前加入“&”,则标题中的该字母会带有下划线,该字母就成为快捷键。当按下Alt+该快捷键,便可激活并操作它。
                                      ToolTipText  当指向该按钮时,出现文字提示。
 
基本数据类型  P39
                       整型   %                            长整型  &
                       单精度型  !                      双精度型  #
                       字符型  $                            变体型   Variant
 
变量  命名规则    P41
常量                   P44
 
运算符和表达式    P45
常用的数学函数    P50
常用的转换函数    P51
字符串函数           P52
日期函数              P53       课上补充内容:WeekDay (C︱D)  返回星期编号,星期日为一,星期一为二。
                                     WeekDayName (N) 返回星期名称
                                                                其中N指数字。
Shell 函数(调用可执行程序)  P54
 
VB的程序结构和编码规则         P55
 
 
 
输入对话框 InputBox函数     P62           函数返回的是用户输入的值(文本,类型为字符串)
 
消息对话框 MsgBox 函数      P63           有两种形式:
函数形式
                                                              变量=MsgBox ( 提示[,按钮][,标题] )
                                                                              过程形式
                                                              MsgBox提示[,按钮][,标题]
 
Print 方法                    P66
(注意定位函数、分隔符的区别和用法)
                                          Spc(n)    插入空格
                                          Tab(n)    定位
 
                                          分号    从上一个字符后接着打
                                          逗号    隔14列打
                                          无      换行
 
Format 格式输出函数          P67
 
 
IF 条件语句                  P69         语句形式:
                                         If               Then
                                        
                                         ElseIf            Then
 
                                         Else
 
                                         End If
 
 
Select Case 语句              P73         语句形式:
                                         SelectCase 
                                         Case  
 
                                         Case Else
 
                                         End Select
条件函数 (IIF函数和Choose函数)    P75
 
 
For 循环语句         P77        注意:退出循环时,变量的值为终值加上步长
                                      循环总次数为 Int((终值—初值)/步长 +1)
      
Do … Loop 循环语句   P79
 
退出和结束 Exit、 End   P84
 
 
求最大公约数和最小公倍数   P87
试凑法 枚举法 求解         P88
递推法                     P90
 
 
程序调试                   P91
 
定长数组及声明             P99
动态数组及声明             P100
 
数组的赋值  Array 函数     P103
 
数组求和、求最小值等       P105
选择法排序                 P106
冒泡法排序                 P107
插入、删除数组             P108
 
 
列表框                     P109
组合框                     P112
 
自定义类型 (用户自定义某个变量的多个不同元素、数据)    P116
                                  形式如下:
                                  Type   自定义类型名
                                       元素名   As   数据类型名
                                  End  Type
 
用户自定义过程,可供其他事件过程调用                     P125
① 自定义函数过程  P126      形式:
                     [Public│Private]  Function 函数过程名(形参列表) [As 类型]
                    
                     End Function
 
② 自定义子过程  P129     形式:
                         [Public│Private] Sub 子过程名([形参列表])
        
                         End Sub
 
 
参数传递         P132
(形参和实参、传地址ByRef与传值ByVal 、 数组参数)
 
过程的嵌套、递归调用    P136
打印杨辉三角形          P137
 
变量的作用域            P140
(局部、模块级、全局)
 
静态变量                P142
 
 
标准控件、ActiveX控件和可插入对象        P151
 
单选按钮、复选框和框架                    P153
 
滚动条ScrollBar 和 进度条 ProgressBar     P156
 
图形框 PictureBox 和 图像框 Image          P159
 
定时器                                    P161
 
蝴蝶飞舞                                  P163
 
 
通用对话框      P163
                位于Microsoft Common Dialog Control 6.0
                包括 打开、另存为、颜色、字体、打印 等对话框
 
菜单设计        P171
                包括下拉式和弹出菜单
 
多重窗体       P175
               如设置启动对象、加载和显示窗体等
 
自动红绿灯模拟程序    P188
 
 
 
补充内容:
 
SSTab 控件    位于Microsoft Tabbed Dialog Control 6.0
 
 
 
 
Updown 控件  位于Microsoft Windows Common Control-2 6.0
              要和text配合使用 (通过属性——同步合作者来设置)
 
 
 
 
工具栏  ToolBar
“Microsoft Windows Common Controls 6.0”将控件添加到工具箱,
通过ToolBar、ImageList组合使用建立工具栏,步骤:
    (1)在ImageList控件中添加所需的图像。
    (2)在ToolBar控件中创建Button对象。
    (3)在ButtonClick事件中用Select Case语句对各按钮进行相应的编程。
 
状态栏  StatusBar
位于Microsoft Windows Common Controls 6.0
注意:每次插入新的窗格时,索引都会回到1,需加以调整。
 
 
 
再过两周就要VB考试了,我整理了一份课本内容的索引,红色字体部分是比较重要也比较常见的程序,希望能帮助大家熟悉重要知识点的所在,翻起书来也会比较快。因为开卷考可能不让夹带资料,所以只能建议大家在目录上做些记号。这份索引做的比较简单,不足之处希望大家指正。相信我们班里有很多VB能人,希望大家互相帮助,我也很乐意为大家解答问题,可以通过QQ、电子邮件、校内网等等各种途径。最后,希望我们大家能够同心协力,解决VB  ^_^
 
 

相关教程