VB中添加一个按钮点击就可以了
Private Sub Command1_Click()
a = InputBox("请输入分数:")
If IsNumeric(a) And a <= 100 And a >= 0 Then
Select Case a
Case 0 To 59
dd = "E"
Case 60 To 69
dd = "D"
Case 70 To 79
dd = "C"
Case 80 To 89
dd = "B"
Case 90 To 100
dd = "A"
Case Else
dd = "成绩错误!"
End Select
MsgBox dd
Else
MsgBox "你输入的成绩非法"
End If
End Sub
排序的各类很多,最简单的冒泡排序代码如下:
Private Sub Command1_Click()
Dim a(), i As Integer, j As Integer, tmp As Integer
a = Array(1, 3, 2, 5, 4, 6, 9, 7, 8, 0)
Print Join(a, ",")
For i = 0 To UBound(a)
For j = i + 1 To UBound(a)
If a(j) < a(i) Then
tmp = a(j)
a(j) = a(i)
a(i) = tmp
End If
Next
Next
Print Join(a, ",")
End Sub
Dim AA(1 To 10) As Integer, ZGCJ(1 To 10) As Integer, ZDCJ(1 To 10) As Integer
在通用部分声明三个数组
Private Sub Command1_Click()Text1Text = "": Text2Text = "": Text3Text = ""
Text1Text = "系统自动生成的十个数:" & vbCrLf
For I = 1 To 10
AA(I) = 0: ZGCJ(I) = 0: ZDCJ(I) = 0
Randomize
AA(I) = Int(Rnd 90 + 10)
Text1Text = Text1Text & AA(I) & Space(4)
If I Mod 5 = 0 Then Text1Text = Text1Text & vbCrLf
ZGCJ(I) = AA(I)
ZDCJ(I) = AA(I)
Next I
End Sub
生成十个数的代码
Private Sub Command2_Click()Text2Text = "": Text3Text = ""
Dim AAA As Integer, BBB As Integer
For I = 1 To 9
For J = I + 1 To 10
If ZGCJ(I) < ZGCJ(J) Then
AAA = ZGCJ(I)
ZGCJ(I) = ZGCJ(J)
ZGCJ(J) = AAA
End If
If ZDCJ(J) < ZDCJ(I) Then
BBB = ZDCJ(J)
ZDCJ(J) = ZDCJ(I)
ZDCJ(I) = BBB
End If
Next J
Next I
Text2Text = Text2Text & "从大到小排列:" & vbCrLf
For I = 1 To 10
Text2Text = Text2Text & ZGCJ(I) & Space(4)
If I Mod 5 = 0 Then Text2Text = Text2Text & vbCrLf
Next I
Text3Text = Text3Text & "从小到大排列:" & vbCrLf
For I = 1 To 10
Text3Text = Text3Text & ZDCJ(I) & Space(4)
If I Mod 5 = 0 Then Text3Text = Text3Text & vbCrLf
Next I
End Sub
排序的代码。
如果需要自己输入数字,可以这样:
'如果要自己输入数字,可以修改下面的代码Text1Text = "": Text2Text = "": Text3Text = ""
Text1Text = "系统自动生成的十个数:" & vbCrLf
For I = 1 To 10
AA(I) = 0: ZGCJ(I) = 0: ZDCJ(I) = 0
Randomize
AA(I) = Int(Rnd 90 + 10)
Text1Text = Text1Text & AA(I) & Space(4)
If I Mod 5 = 0 Then Text1Text = Text1Text & vbCrLf
ZGCJ(I) = AA(I)
ZDCJ(I) = AA(I)
Next I
'----------------------------修改为:
Text1Text = "": Text2Text = "": Text3Text = ""
Text1Text = "用户输入的十个数:" & vbCrLf
For I = 1 To 10
AA(I) = 0: ZGCJ(I) = 0: ZDCJ(I) = 0
AA(I) = Val(InputBox("请输入第" & I & "个数!"))
Text1Text = Text1Text & AA(I) & Space(4)
If I Mod 5 = 0 Then Text1Text = Text1Text & vbCrLf
ZGCJ(I) = AA(I)
ZDCJ(I) = AA(I)
Next I
Private Sub Command1_Click()
Dim 单号() As Integer, 单号索引 As Integer
Dim 双号() As Integer, 双号索引 As Integer
单号索引 = -1
双号索引 = -1
For i = 1 To 10
a = 生成2位随机数
If a Mod 2 = 1 Then
单号索引 = 单号索引 + 1
ReDim Preserve 单号(单号索引)
单号(单号索引) = a
Else
双号索引 = 双号索引 + 1
ReDim Preserve 双号(双号索引)
双号(双号索引) = a
End If
Next
数组排序 单号
DebugPrint "----以下是单号----"
For i = 0 To UBound(单号)
DebugPrint 单号(i)
Next
数组排序 双号, True
DebugPrint "----以下是双号----"
For i = 0 To UBound(双号)
DebugPrint 双号(i)
Next
End Sub
Public Function 生成2位随机数()
Randomize Timer
生成2位随机数 = Int(90 Rnd) + 10
End Function
Public Sub 数组排序(ByRef MyArray As Variant, Optional ByVal Order As Boolean)
Dim Distance, Size, Index, NextElement, TEMP, gIterations
Size = UBound(MyArray) - LBound(MyArray) + 1: Distance = 1: While (Distance <= Size): Distance = 2 Distance: Wend
Distance = (Distance / 2) - 1: While (Distance > 0): NextElement = LBound(MyArray) + Distance
While (NextElement <= UBound(MyArray)): Index = NextElement: Do: If Index < (LBound(MyArray) + Distance) Then Exit Do
If Not Order Then
If MyArray(Index) >= MyArray(Index - Distance) Then Exit Do
TEMP = MyArray(Index): MyArray(Index) = MyArray(Index - Distance): MyArray(Index - Distance) = TEMP: Index = Index - Distance: gIterations = gIterations + 1
ElseIf Order Then
If MyArray(Index) < MyArray(Index - Distance) Then Exit Do
TEMP = MyArray(Index): MyArray(Index) = MyArray(Index - Distance): MyArray(Index - Distance) = TEMP: Index = Index - Distance: gIterations = gIterations + 1
End If: Loop: NextElement = NextElement + 1: gIterations = gIterations + 1: Wend: Distance = (Distance - 1) / 2: gIterations = gIterations + 1: Wend
End Sub
=============执行结果============
----以下是单号----
15
61
63
75
79
87
----以下是双号----
96
88
80
76
1、选择排序发(从小到大)
Private Sub Command1_Click()
Dim a(10) As Integer
For i = 1 To 10
Randomize
a(i) = Int(Rnd 90) + 10
Next i
For i = 1 To 9
For j = i + 1 To 10
If a(i) > a(j) Then
r = a(i)
a(i) = a(j)
a(j) = r
End If
Next j
Next i
For i = 1 To 10
Print a(i);
Next i
End Sub
2、冒泡排序法
Private Sub Command1_Click()
Dim a(10) As Integer
For i = 1 To 10
Randomize
a(i) = Int(Rnd 90) + 10
Next i
For i = 1 To 9
For j = 1 To 10 - i
If a(j) > a(j + 1) Then
r = a(j)
a(j) = a(j + 1)
a(j + 1) = r
End If
Next j
Next i
For i = 1 To 10
Print a(i);
Next i
End Sub
冒泡排序法思路:(假设有10个数,需要从小到大排)
Dim a(10)
Randomize
For i = 1 To 10
a(i) = Int(Rnd i + 3 + Rnd 10) '给数组赋值
Print a(i);
Next i
For i = 1 To 10 ‘
For j = i + 1 To 10
If a(j) < a(i) Then ’如果剩余的比a(i)小的话,就交换
t = a(j)
a(j) = a(i)
a(i) = t
End If
Next j
Next i
For k = 1 To 10
Print a(k); ‘打印排好的数
Next k
、
冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法。
它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。
这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端,故名。
Option Explicit
Private Sub Command1_Click() '排序
Dim i%, j%, S() As String, t As String
ReDim S(Len(Trim(Text1)))
For i = LBound(S) To UBound(S)
S(i) = Mid(Trim(Text1), i + 1, 1)
Next
For i = 0 To UBound(S)
For j = i + 1 To UBound(S)
If S(j) < S(i) Then t = S(i): S(i) = S(j): S(j) = t
Next
Text2 = Text2 & S(i)
Text2 = Trim(Text2)
Next
End Sub
Private Sub Command2_Click() '读入
Dim Txt As String
Open AppPath & "\test12txt" For Input As #11
Do While Not EOF(11)
Line Input #11, Txt
Text1 = Text1 & Txt
Loop
Close #11
End Sub
Private Sub Command3_Click() '追加
Open AppPath & "\test12txt" For Append As #12
Print #12, Text2
Close #12
End Sub
以上就是关于用VB怎么做学生成绩等级排序的程序全部的内容,包括:用VB怎么做学生成绩等级排序的程序、VB的排序法的代码、VB中输入10个数并按要求进行升序排序和降序的程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)