c#窗体设计素数计算
时间: 2023-07-27 15:22:18 浏览: 60
下面是一个简单的 C# 窗体程序,可以计算一个整数是否为素数:
```csharp
using System;
using System.Windows.Forms;
namespace PrimeNumberChecker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void checkButton_Click(object sender, EventArgs e)
{
int number = int.Parse(numberTextBox.Text);
if (IsPrime(number))
{
resultLabel.Text = $"{number} is a prime number.";
}
else
{
resultLabel.Text = $"{number} is not a prime number.";
}
}
private bool IsPrime(int number)
{
if (number < 2)
{
return false;
}
for (int i = 2; i <= Math.Sqrt(number); i++)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
}
}
```
在窗体设计器中,可以添加一个 TextBox 控件和一个 Label 控件,以及一个 Button 控件,然后在 Button 的 Click 事件中调用 IsPrime 方法来检查输入的整数是否为素数。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)