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