- 此问题在此处已有答案**:
Why do I only see some of my text output when my Window Forms application has a loop?(1个答案)
七年前就关门了。
我尝试在Windows窗体应用程序中组合质数、偶数和奇数及其结果。我已经在控制台中测试了代码,但在Windows窗体中它不会循环到下一个适用的数字。例如:在控制台1 - 10中,质数将导致"2,3,5,7",但在Windows窗体应用程序中,将导致"2"
public partial class NumberCalc : Form
{
public NumberCalc()
{
InitializeComponent();
}
private void Primes_CheckedChanged(object sender, EventArgs e)
{
{
int f = Convert.ToInt32(Min.Text);
int i = Convert.ToInt32(Max.Text);
bool isPrime = true;
for (f = 0; f <= i; f++)
{
for (int j = 2; j <= i; j++)
{
if (f != j && f % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
string final;
final = ("The Prime Numbers Are:" + f);
Result.Text = final;
}
isPrime = true;
}
}
}
private void Result_TextChanged(object sender, EventArgs e)
{
}
private void Min_TextChanged(object sender, EventArgs e)
{
}
private void Evens_CheckedChanged(object sender, EventArgs e)
{
int f = Convert.ToInt32(Min.Text);
int i = Convert.ToInt32(Max.Text);
for (f = 0; f >= i; f++)
{
if (f % 2 == 0)
{
{
string final;
final = ("The Even Numbers Are:" + f);
Result.Text = final;
}
}
}
}
private void Odds_CheckedChanged(object sender, EventArgs e)
{
int f = Convert.ToInt32(Min.Text);
int i = Convert.ToInt32(Max.Text);
for (f = 0; f <= i; f++)
{
if (f % 2 != 0)
{
{
string final;
final = ("The Even Numbers Are:" + f);
Result.Text = final;
}
}
}
}
}
3条答案
按热度按时间w80xi6nr1#
将代码更改为:
偶数和奇数是一样的。顺便说一句,1不是质数,对吗?
e37o9pze2#
我会将您的循环/检查组合成一个方法,如下所示:
vsdwdz233#
我认为LINQ更适合这里,你可以试试这个:
Prime编号更新:
其中
IsPrime()
方法定义如下?