重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
如果你是从vb6刚过渡上vb。net,建议还是用冒泡排序法,容易理解。
创新互联公司公司2013年成立,先为上犹等服务建站,上犹等地企业,进行企业商务咨询服务。为上犹企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
如果你正努力学习vb。net的方法,推荐一个例子如下:
Imports System
Imports System.Collections
Public Class SamplesArray
Public Class myReverserClass
Implements IComparer
' Calls CaseInsensitiveComparer.Compare with the parameters reversed.
Function Compare(x As Object, y As Object) As Integer _
Implements IComparer.Compare
Return New CaseInsensitiveComparer().Compare(y, x)
End Function 'IComparer.Compare
End Class 'myReverserClass
Public Shared Sub Main()
' Creates and initializes a new Array and a new custom comparer.
Dim myArr As [String]() = {"The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog"}
Dim myComparer = New myReverserClass()
' Displays the values of the Array.
Console.WriteLine("The Array initially contains the following values:")
PrintIndexAndValues(myArr)
' Sorts a section of the Array using the default comparer.
Array.Sort(myArr, 1, 3)
Console.WriteLine("After sorting a section of the Array using the default comparer:")
PrintIndexAndValues(myArr)
' Sorts a section of the Array using the reverse case-insensitive comparer.
Array.Sort(myArr, 1, 3, myComparer)
Console.WriteLine("After sorting a section of the Array using the reverse case-insensitive comparer:")
PrintIndexAndValues(myArr)
' Sorts the entire Array using the default comparer.
Array.Sort(myArr)
Console.WriteLine("After sorting the entire Array using the default comparer:")
PrintIndexAndValues(myArr)
' Sorts the entire Array using the reverse case-insensitive comparer.
Array.Sort(myArr, myComparer)
Console.WriteLine("After sorting the entire Array using the reverse case-insensitive comparer:")
PrintIndexAndValues(myArr)
End Sub 'Main
Public Shared Sub PrintIndexAndValues(myArr() As [String])
Dim i As Integer
For i = 0 To myArr.Length - 1
Console.WriteLine(" [{0}] : {1}", i, myArr(i))
Next i
Console.WriteLine()
End Sub 'PrintIndexAndValues
End Class 'SamplesArray
'This code produces the following output.
'
'The Array initially contains the following values:
' [0] : The
' [1] : QUICK
' [2] : BROWN
' [3] : FOX
' [4] : jumps
' [5] : over
' [6] : the
' [7] : lazy
' [8] : dog
'
'After sorting a section of the Array using the default comparer:
' [0] : The
' [1] : BROWN
' [2] : FOX
' [3] : QUICK
' [4] : jumps
' [5] : over
' [6] : the
' [7] : lazy
' [8] : dog
'
'After sorting a section of the Array using the reverse case-insensitive comparer:
' [0] : The
' [1] : QUICK
' [2] : FOX
' [3] : BROWN
' [4] : jumps
' [5] : over
' [6] : the
' [7] : lazy
' [8] : dog
'
'After sorting the entire Array using the default comparer:
' [0] : BROWN
' [1] : dog
' [2] : FOX
' [3] : jumps
' [4] : lazy
' [5] : over
' [6] : QUICK
' [7] : the
' [8] : The
'
'After sorting the entire Array using the reverse case-insensitive comparer:
' [0] : the
' [1] : The
' [2] : QUICK
' [3] : over
' [4] : lazy
' [5] : jumps
' [6] : FOX
' [7] : dog
' [8] : BROWN
第一题:
不需要任何控件,代码如下:
Private Sub Form_Click()
Dim A() As Integer, N As Integer
Dim St As String, I As Integer, J As Integer
Randomize
Do
St = InputBox("数字的个数", "输入", Int(Rnd * 100))
If St = "" Then
MsgBox "请输入数字!"
Else
N = Int(Val(St))
If N 1 Then
MsgBox "请输入大于0的数字!"
Else
Exit Do
End If
End If
Loop
ReDim A(N)
For I = 1 To N
Do
St = InputBox("第" + Str(I) + "个数字", "输入", Int(Rnd * 100))
If St = "" Then
MsgBox "请输入数字!"
Else
A(I) = Int(Val(St))
Exit Do
End If
Loop
Next
For I = 1 To N - 1
For J = I + 1 To N
If A(I) A(J) Then
A(0) = A(I)
A(I) = A(J)
A(J) = A(0)
End If
Next
Next
For I = 1 To N
Open App.Path "\" Trim(Str(I)) ".txt" For Output As #1
Print #1, A(I)
Close #1
Next
Print "已经把"; N; "个数写入到"; App.Path; "\1.txt 到 "; N; ".txt中.请查看."
End Sub
'已经运行过.
第二题:
DIM 是变量声明语句,它的格式为:
dim 变量名[as 格式] [,变量名[as 格式][,变量名[as 格式]......]
其中:
变量名:以字母或汉字开始的字串,代表一个变量
格式有以下几种:
属于数字的有五种:
(1)字节型:byte可取值0-255
(2)整形:integer可取值-32768至32767
(3)长整形:long(可取值范围很大的正负整数)
(4)单精度型:single(可取值小数)
(5)双精度型:double(可取值范围更大,小数位数更多的小数)
字符串型:string(可代表由字母\数字或汉字组成的字符集合)
布尔型:boolean(取值为ture\false)
日期型:date(可表示形如2009-5-26 02:36这样的组合)
如果要用姓名\住址\单位名称...等用字符串型(string)
eg:dim name as string(用name变量表示名字时,声明成字符串变量)
如果是用数字需要做计算,如工资\合计\人数....等要用数字型,但有一个原则,优先选用范围小的(按照字节型(byte)\整形(integer)\长整形(long)\单精度型(single)\双精度型(double)的顺序选择),够用就可以了,这样可以占用内存少,运算速度快.
eg:dim count as integer(用integer表示员工人数时,可声明成整形变量)
eg:dim sum as single(用sum表示工资时,可声明成单精度型变量)
不知是否说得清楚了.
刚回答过一个,给你一个参考!
Public Class Form1
Dim A(2, 4) As Integer '这里声名的二维是3和5是从0算起的
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text="" '清空文本
'Dim A(2, 4) As Integer '也可以在这里面声明。
Dim i As Integer : Dim j As Integer
For i = 0 To 2 '从0算起到2就是3次计数。
For j = 0 To 4 '从0算起到4就是5次计数。
A(i, j) = Int(Rnd() * 400 - 200)'随机生成一个号码
TextBox1.Text = A(i, j) " " '把一行的5个数链接排出来。
Next
TextBox1.Text = vbCrLf '添加换行
Next
End Sub
End Class
Dim d() As Integer
Dim n As Integer
Dim i As Integer
Dim j As Integer
Dim T As Integer
n = Val(InputBox("请输入n,确定数组的元素个数"))
ReDim d(n)
'用随机函数生成正整数数组
Randomize
For i = 1 To n
d(i) = Int(Rnd * 100 + 1)
Next i
'输出
For i = 1 To n
Print d(i),
Next i
'从小到大排序
For i = 1 To n - 1
For j = i + 1 To n
If d(i) d(j) Then
T = d(i)
d(i) = d(j)
d(j) = T
End If
Next j
Next i
'输出
For i = 1 To n
Print d(i),
Next i
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim N As Integer = 9
Randomize()
Dim xx(9) As Integer
For i As Integer = 0 To N
xx(i) = Int(Rnd() * 1000) '产生 [0,1000) 之间的整数
Next
For i As Integer = 0 To N - 1
For j As Integer = i + 1 To N
If xx(i) xx(j) Then
Dim temp As Integer = 0
temp = xx(i) : xx(i) = xx(j) : xx(j) = temp
End If
Next
Next
Dim sum As Integer = 0
For i As Integer = 0 To N
TextBox1.Text += xx(i) vbCrLf
sum += xx(i)
Next
TextBox1.Text += "最大值:" xx(9) vbCrLf "最小值:" xx(0) vbCrLf "平均值:" sum / 10
End Sub
End Class