C# 仅在NumericUpDown控件中显示素数
我想知道是否有一种方法可以在数字上下显示素数。我正在尝试为大学制作密码术,我只需要为用户提供素数选项。这里有:它只显示素数C# 仅在NumericUpDown控件中显示素数,c#,winforms,numbers,primes,C#,Winforms,Numbers,Primes,我想知道是否有一种方法可以在数字上下显示素数。我正在尝试为大学制作密码术,我只需要为用户提供素数选项。这里有:它只显示素数 private void numericUpDown1_ValueChanged_1(object sender, EventArgs e) { //my numeric up down var name is numrsa1// } static bool IsPrimeNumber(int value) {
private void numericUpDown1_ValueChanged_1(object sender, EventArgs e)
{
//my numeric up down var name is numrsa1//
}
static bool IsPrimeNumber(int value)
{
bool result = true;
for (int i = 2; i < value; i++)
{
if (value % i == 0)
{
result = false;
break;
}
}
if (value <= 1)
result = false;
return result;
}`
//剩下的是您的代码,没有任何更改,我使用您的函数声明IsPrimeNumber来验证数字并在那里打印结果。它将暂时上升,并通过按升序增加控件的值仅显示一个素数
这是一个非常简单的解决方案,我可以测试得足够快,以便尽快发布:因为您要求的功能类似于NumericUpDown控件,但具有不同的行为,因此我建议您创建一个扩展NumericUpDown控件的新控件PrimeNumberUpDown 这里有一个快速版本,我一起看。我使用鼠标或箭头键测试了它的上/下功能,并在文本框中输入了一个数字。如果它涉及到任何关键问题,您可能需要更彻底地测试它 将此代码粘贴到一个新文件中,并相应地更新名称空间、编译,然后您应该会在工具箱中看到PrimeNumberUpDown控件,这样您就可以像使用任何其他Windows窗体控件一样使用它
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
//suposing you have this code
int numrsa1 = (int)this.numericUpDown2.Value;
//or add the rest 8 lines to your code
if (IsPrimeNumber(numrsa1))
{
numericUpDown2.Value = numrsa1;
}
else
{
numericUpDown2.UpButton();
}
}
大家好,欢迎光临。请阅读这里如何提问。你应该详细说明你做了什么以及你遗漏了什么,以便其他人可以帮助你。Ashkan我什么都没有yeat,因为我不知道我需要使用什么类型的o逻辑。向上向下是指降序吗?您可以将上一个值存储在tag属性中,然后检查它是低还是高,然后计算下一个素数并设置数字up down的值,而改用DomainUpDown控件。这就像一个数字向上向下,但是你可以用你自己的集合来填充,在你的例子中,这些集合只是素数。
using System;
using System.Globalization;
using System.Windows.Forms;
namespace YourNameSpace
{
public class PrimeNumberUpDown : NumericUpDown
{
private int _value;
public PrimeNumberUpDown()
{
// Make sure default value is prime
if (!IsPrime((int)Value))
SetNextPrimeValue();
_value = (int)Value;
}
public override void DownButton()
{
SetNextPrimeValue();
}
public override void UpButton()
{
SetPreviousPrimeValue();
}
private void SetNextPrimeValue()
{
int newValue = (int)Value;
while (newValue <= Maximum)
{
if (IsPrime(++newValue))
{
if (newValue <= Maximum)
{
Value = newValue;
_value = newValue;
}
return;
}
}
}
private void SetPreviousPrimeValue()
{
int newValue = (int)Value;
while (newValue >= Minimum)
{
if (IsPrime(--newValue))
{
if (newValue >= Minimum)
{
Value = newValue;
_value = newValue;
}
return;
}
}
}
protected override void ValidateEditText()
{
if (_value == 0)
{
base.ValidateEditText();
return;
}
int newValue;
if (int.TryParse(Text, out newValue) && IsPrime(newValue) && newValue >= Minimum && newValue <= Maximum)
{
_value = newValue;
base.ValidateEditText();
}
else
{
ChangingText = true;
Text = _value.ToString(CultureInfo.InvariantCulture);
}
}
private static bool IsPrime(int number)
{
if (number == 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
int boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i += 2)
{
if (number % i == 0) return false;
}
return true;
}
}
}