在文本框textBox1中输入一个正整数,单击“判断”按钮,就可以判断输入的数是否为素数,

2025-01-06 19:42:13
推荐回答(3个)
回答1:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim m, i As Long
i = 0
m = Val(TextBox1.Text)
For i = 2 To m - 1
If (m Mod i) = 0 Then
Label1.Text = "这个数不是素数"
Exit For
End If
Next
If i = m Then Label1.Text = "这个数是素数"
End Sub

回答2:

Private Sub command_Click
Dim m, i As Long
i = 0
m = Val(TextBox1.Text)
For i = 2 To m \2
If (m Mod i) = 0 Then
Label1.Text = "这个数不是素数"
Exit For
End If
Next
If i = m Then Label1.Text = "这个数是素数"
End Sub

回答3:

public MainWindow()
{
InitializeComponent();
}
Boolean b = true;
private void 判断_Click(object sender, RoutedEventArgs e)
{
int s = int.Parse(textBox1.Text);
for (int i = 2; i < s / 2; i++)
{
if (s % i == 0)
b = false;
}
if (b) MessageBox.Show(s + "是素数");
else MessageBox.Show(s + "不是素数");
}