早教吧 育儿知识 作业答案 考试题库 百科 知识分享

vb:输入m,n后再输入a数组的m个数和b数组的n个数,显示a、b中不同的数(如输入a组1.2.3.6,b组2.3.5,输出结果为1.6和5)PrivateSubFormClick()DimnAsByte,mAsByte,amAsByte,bnAsByte,a()AsInteger,b()AsIntegerm

题目详情
vb:输入m,n后再输入a数组的m个数和b数组的n个数,显示a、b中不同的数(如输入a组1.2.3.6,b组2.3.5,输出结果为1.6和5)
Private Sub Form_Click()
Dim n As Byte,m As Byte,am As Byte,bn As Byte,a() As Integer,b() As Integer
m = InputBox("a数组中元素个数"):n = InputBox("b数组中元素个数")
ReDim a(1 To m)
For am = 1 To m
a(am) = Val(InputBox("a(" & m & ")="))
Next am
ReDim b(1 To n)
For bn = 1 To n
a(bn) = Val(InputBox("a(" & n & ")="))
Next bn
For am = 1 To m
For bn = 1 To n
If a(am) - b(bn) 0 Then Exit For
Next bn
Print a(am);
Next am
For bn = 1 To n
For am = 1 To m
If b(bn) - a(am) 0 Then Exit For
Next am
Print b(bn);
Next bn
End Sub
做得是错的,还乱糟糟的,
▼优质解答
答案和解析
libbo1974网友的做法是完全正确的,但没有指出程序中所有的错误,我来一个完整版吧.引用了libbo1974网友的程序,在此谢谢.还有为什么要用Byte这种数据类型呢,它只能表示0~255,改成了Integer类型.
Private Sub Form_Click()
Dim n As integer, m As Integer, am As Integer, bn As Integer, a() As Integer, b() As Integer
Dim isSame As Boolean
m = InputBox("a数组中元素个数")
n = InputBox("b数组中元素个数")
ReDim a(1 To m)
For am = 1 To m
a(am) = Val(InputBox("a(" & am & ")="))
Next
ReDim b(1 To n)
For bn = 1 To n
b(bn) = Val(InputBox("b(" & bn & ")="))
Next
For am = 1 To m
isSame = False
For bn = 1 To n
If a(am) - b(bn) = 0 Then isSame = True
Next
If isSame = False Then Print a(am)
Next
For bn = 1 To n
isSame = False
For am = 1 To m
If b(bn) - a(am) = 0 Then isSame = True
Next
If isSame = False Then Print b(bn)
Next
End Sub