1设计一个的程序,先在edit1和edit2控件中分别输入任意两个整数,,在edit3控件中输出两个数的最大公约数 5
1个回答
展开全部
新建一个VB工程:然后拉上3个文本框,一个按钮
将以下代码复制上去即可:
Private Sub Command1_Click()
Dim count1 As Integer
Dim count2 As Integer
If Not IsNumeric(Text1.Text) Then
MsgBox "请输入数字"
Text1.Text = ""
Exit Sub
End If
If Not IsNumeric(Text2.Text) Then
MsgBox "请输入数字"
Text2.Text = ""
Exit Sub
End If
count1 = Text1.Text
count2 = Text2.Text
Text3.Text = max_gy(count1, count2)
End Sub
Function max_gy(a As Integer, b As Integer) '//求最大公约数的函数
Dim i As Integer
If a < b Then
For i = 1 To a
If a Mod i = 0 And b Mod i = 0 Then
max_gy = i
End If
Next i
Else
For i = 1 To b
If a Mod i = 0 And b Mod i = 0 Then
max_gy = i
End If
Next i
End If
End Function
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
将以下代码复制上去即可:
Private Sub Command1_Click()
Dim count1 As Integer
Dim count2 As Integer
If Not IsNumeric(Text1.Text) Then
MsgBox "请输入数字"
Text1.Text = ""
Exit Sub
End If
If Not IsNumeric(Text2.Text) Then
MsgBox "请输入数字"
Text2.Text = ""
Exit Sub
End If
count1 = Text1.Text
count2 = Text2.Text
Text3.Text = max_gy(count1, count2)
End Sub
Function max_gy(a As Integer, b As Integer) '//求最大公约数的函数
Dim i As Integer
If a < b Then
For i = 1 To a
If a Mod i = 0 And b Mod i = 0 Then
max_gy = i
End If
Next i
Else
For i = 1 To b
If a Mod i = 0 And b Mod i = 0 Then
max_gy = i
End If
Next i
End If
End Function
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
参考资料: 用VB测试了,可以用!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询