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

用VB编程.1.输入一个整数n,计算n的阶乘.2.输入一个大于2的偶数,验证它可以表示成两个质数之和.3.生成一个序列,判断该序列是否为有序序列.

题目详情
用VB编程.
1.输入一个整数n,计算n的阶乘.
2.输入一个大于2的偶数,验证它可以表示成两个质数之和.
3.生成一个序列,判断该序列是否为有序序列.
▼优质解答
答案和解析
三个文本框,三个控制按钮:
Private Function sushu(x As Integer) As Boolean
Dim i As Integer
For i = 2 To x - 1
If x Mod i = 0 Then Exit Function
Next i
sushu = True
End Function
Private Sub Command1_Click()
Dim n As Integer, i As Integer, x As Integer
n = InputBox("请出入n")
x = 1
For i = 1 To n
x = x * i
Next i
Text1.Text = CStr(x)
End Sub
Private Sub Command2_Click()
Dim i As Integer, x As Integer
x = InputBox("请输入一个大于2的数")
If x > 2 Then
For i = 1 To x / 2
If sushu(i) And sushu(x - i) Then
Text2.Text = x & "= " & i & " + " & x - i
Exit For
End If
Next i
Else
Text2.Text = "请重新输入一个大于2的数"
End If
End Sub
Private Sub Command3_Click()
Dim x As Integer, i As Integer, a() As Integer, j As Integer, k As Integer, m As Boolean, n As Boolean
Text3.Text = ""
x = InputBox("请输入序列长度")
ReDim a(x)
For i = 1 To x
a(i) = Int(Rnd * (99 - 10 + 1) + 10)
Text3.Text = Text3.Text & a(i) & " "
Next i
For i = 1 To x - 1
If a(i) < a(i + 1) Then j = j + 1
Next i
If j = x - 1 Then
m = True
Else
m = False
End If
For i = 1 To x - 1
If a(i) > a(i + 1) Then k = k + 1
Next i
If k = x - 1 Then
n = True
Else
n = False
End If
If m Or n = True Then
Text3.Text = Text3.Text & vbCrLf & "是有序数列"
Else
Text3.Text = Text3.Text & vbCrLf & "不是有序数列"
End If
End Sub