程序运行后在两个文本框中分别输入正整数a和b(a<=b)单击确定按钮,在文本框中显示ab间所有素数

VB程序

Public Class Form1

    Function isprime(ByVal x As Integer) As Integer

        Dim i As Integer

        For i = 2 To x - 1

            If x Mod i = 0 Then

                Exit For

            End If

        Next i

        If i >= x Then

            isprime = 1

        Else

            isprime = 0

        End If


    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Label1.Text = ""

        Dim a As Integer

        Dim b As Integer

        a = TextBox1.Text

        b = TextBox2.Text

        Dim x As Integer

        For x = a To b

            If isprime(x) = 1 Then

                Label1.Text = Label1.Text + x.ToString() + ","

            End If

        Next x

    End Sub

End Class

追问

显示语法错误啊

追答

什么错误?你双击button1再把button1下的代码复制过去 不要全部复制过去啦

追问

 这是什么问题??

追答

这个样子,把你第一行和最后一行的删了,看看还有哪里不一样

追问

还是不行啊

追答

我给你的是对的,你自己贴的不对,你重新建个项目,2个textbox,1个label,1个button,双击button,粘贴这段代码到里面
Label1.Text = ""
Dim a As Integer
Dim b As Integer
a = TextBox1.Text
b = TextBox2.Text
Dim x As Integer
For x = a To b
If isprime(x) = 1 Then
Label1.Text = Label1.Text + x.ToString() + ","
End If
Next x
End Sub
然后再在button的外面粘贴这段代码
Function isprime(ByVal x As Integer) As Integer
Dim i As Integer
For i = 2 To x - 1
If x Mod i = 0 Then
Exit For
End If
Next i
If i >= x Then
isprime = 1
Else
isprime = 0
End If

End Function

温馨提示:答案为网友推荐,仅供参考
相似回答