VB高手帮忙看看!
答案:4 悬赏:20 手机版
解决时间 2021-06-06 18:43
- 提问者网友:骨子里的高雅
- 2021-06-06 15:15
Private Sub Command1_Click()
Dim Text1, a, b As Integer
Dim N As Long, Flags As Boolean
m = Val(Text1)
a = 2
Do
N = Val(a)
Flags = True
For i = 2 To Sqr(N)
If N Mod i = 0 Then
Flags = False
Exit For
End If
Next
p = IIf(Flags, "1", "2")
t = Val(p)
If t = 1 Then
b = m - a
N = Val(b)
Flags = True
For i = 2 To Sqr(N)
If N Mod i = 0 Then
Flags = False
Exit For
End If
Next
p = IIf(Flags, "1", "2")
t = Val(p)
If t = 1 Then
Text2 = m & "=" & a & "+" & b
End If
End If
Loop Until a > m
End Sub
帮我看看上面这一段代码哪里出错了
目的是验证哥德巴赫猜想
最佳答案
- 五星知识达人网友:摆渡翁
- 2021-06-06 16:04
Dim Text1, a, b As Integer
写成:
Dim a As Integer , b As Integer
全部回答
- 1楼网友:零点过十分
- 2021-06-06 19:25
这个代码运行后可能会造成内存溢出的啊,计算量太大了
Private Sub Command1_Click()
Dim Text1, a, b As Integer
Dim N As Long, Flags As Boolean
m = Val(Text1) '这句的m定义了没,还有Val(Text1) 是Val(Text1.text)
吧。
a = 2
Do
N = Val(a)
Flags = True
For i = 2 To Sqr(N)
If N Mod i = 0 Then
Flags = False
Exit For
End If
Next
p = IIf(Flags, "1", "2")
t = Val(p)
If t = 1 Then
b = m - a
N = Val(b)
Flags = True
For i = 2 To Sqr(N)
If N Mod i = 0 Then
Flags = False
Exit For
End If
Next
p = IIf(Flags, "1", "2")
t = Val(p)
If t = 1 Then
Text2 = m & "=" & a & "+" & b '这句应该是Text2.text = m & "=" & a & "+" & b
'才可显示吧
End If
End If
Loop Until a > m
End Sub
哥们真没看出来你是怎么验证的,下边是个实例,希望对你有帮助
'放置一个command1,一个text1,一个label1,使用它们默认的名字,验证哥德巴赫猜想
Private Sub Command1_Click()
Dim N As Long, I As Long, J As Long, K As Long
If IsNumeric(Text1.Text) Then
N = CLng(Text1.Text)
If N Mod 1 = 0 And N >= 9 Then
For I = 3 To N \ 2
If Pd(I) Then
For J = 3 To N - I
If Pd(J) Then
K = N - I - J
If Pd(K) Then
Label1.Caption = "结果: " & N & "=" & I & "+" & J & "+" & K & "符合!"
Exit Sub
End If
End If
Next
End If
Next
Label1.Caption = "结果: 不符合!"
Else
Text1.Text = "请输入大于或等于9的奇数!"
End If
Else
Text1.Text = "请输入数字!"
End If
End Sub
Private Sub Form_Load()
Text1.Visible = True
Label1.Caption = ""
Command1.Caption = "验证"
Me.Show
End Sub
Private Sub Text1_GotFocus()
Text1.Text = ""
Text1.ForeColor = &H0
Command1.Visible = True
End Sub
Function Pd(a As Long) As Boolean
Dim b As Long
For b = 2 To a - 1
DoEvents
If a Mod b = 0 Then
Pd = False
Exit For
Else
Pd = True
End If
Next
End Function
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯