输入N个数到数组中,找出其中最小的数和最大的数,并将两者互换位置。
数组元素的个数N用Inputbox函数输入单击窗体,运行结果:数组的初始值:176-992交换后数组的值:1-99672...
数组元素的个数N用Inputbox函数输入
单击窗体,运行结果:
数组的初始值:
1 7 6 -99 2
交换后数组的值:
1 -99 6 7 2 展开
单击窗体,运行结果:
数组的初始值:
1 7 6 -99 2
交换后数组的值:
1 -99 6 7 2 展开
1个回答
展开全部
Option Explicit
Private Sub Form_Click()
Dim n, i As Integer
Dim a(), temp, x, max, min
Dim maxi, mini As Integer
n = InputBox("请输入元素的个数:", "数据输入", 5)
If n = "" Or IsNumeric(n) = False Then MsgBox "未输入数据或数据输入有误!", 64 + 0, "错误": Exit Sub
ReDim a(1 To n)
Print "数据的初始值是:"
For i = 1 To n
a(i) = InputBox("请输入第" & i & "个元素值", "数据输入")
If a(i) = "" Or IsNumeric(a(i)) = False Then MsgBox "未输入数据或数据输入有误!", 64 + 0, "错误": Exit Sub
Me.Print a(i) & vbTab;
Next
Print
Print "交换后的数据是:"
max = a(1): min = a(1)
For i = 1 To n
If max < a(i) Then max = a(i): maxi = i
If min > a(i) Then min = a(i): mini = i
Next
temp = a(maxi): a(maxi) = a(mini): a(mini) = temp
For i = 1 To n
Print a(i) & vbTab;
Next
End Sub
Private Sub Form_Click()
Dim n, i As Integer
Dim a(), temp, x, max, min
Dim maxi, mini As Integer
n = InputBox("请输入元素的个数:", "数据输入", 5)
If n = "" Or IsNumeric(n) = False Then MsgBox "未输入数据或数据输入有误!", 64 + 0, "错误": Exit Sub
ReDim a(1 To n)
Print "数据的初始值是:"
For i = 1 To n
a(i) = InputBox("请输入第" & i & "个元素值", "数据输入")
If a(i) = "" Or IsNumeric(a(i)) = False Then MsgBox "未输入数据或数据输入有误!", 64 + 0, "错误": Exit Sub
Me.Print a(i) & vbTab;
Next
Print "交换后的数据是:"
max = a(1): min = a(1)
For i = 1 To n
If max < a(i) Then max = a(i): maxi = i
If min > a(i) Then min = a(i): mini = i
Next
temp = a(maxi): a(maxi) = a(mini): a(mini) = temp
For i = 1 To n
Print a(i) & vbTab;
Next
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询