本文介绍了为什么循环不会在我的Windows窗体应用程序中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在Windows窗体应用程序中结合素数,偶数和奇数以及它们的结果。我已经在控制台中测试了代码,但在Windows Form中,它不会循环到下一个适用的编号。例如:在控制台1 - 10素数将导致2,3,5,7,但在Windows窗体应用程序将导致2<$ p
public NumberCalc()
{
InitializeComponent(); $ p $
public partial class NumberCalc:Form
private void Primes_CheckedChanged(object sender,EventArgs e)
{
{
int f = Convert.ToInt32(Min.Text);
int i = Convert.ToInt32(Max.Text);
bool isPrime = true; (f = 0; f
对于(int j = 2; j< = i; j ++)
{
if(f!= j& f%j == 0)
{
isPrime = false;
break;
$ b $ if(isPrime)
{
string final;
final =(素数是:+ f);
Result.Text = final;
}
isPrime = true;
$ private void Result_TextChanged(object sender,EventArgs e)
{
}
private void Min_TextChanged(object sender,EventArgs e)
{
$ b $ private void Evens_CheckedChanged(object sender,EventArgs e)
{
int f = Convert.ToInt32(Min.Text);
int i = Convert.ToInt32(Max.Text); (f = 0; f> = i; f ++)
if(f%2 == 0)
{
{
string final;
final =(偶数:+ f);
Result.Text = final;
private void Odds_CheckedChanged(object sender,EventArgs e)
{
int f = Convert.ToInt32(Min.Text);
int i = Convert.ToInt32(Max.Text); (f = 0; f {
if(f%2!= 0)
{
{
string final;
final =(偶数:+ f);
Result.Text = final;
$ b code $
$ b $我觉得LINQ在这里比较合适,你可以试试这个:
解决方案 int [] numbers = Enumerable.Range(f,if).ToArray< int>();
字符串oddNumbers = string.Join(,,从数字中的数字
其中(数字%2)!= 0
选择数字);
字符串evenNumbers = string.Join(,,从数字中的数字
其中(数字%2)== 0
选择数字);
Result.Text =偶数是:+ evenNumbers;
Result.Text =奇数是:+ oddNumbers;
更新为素数:
<$ p $ ($,$,$,$,$,$,$,$,$,$,$,$,$)
其中 IsPrime()
方法被定义为如下所示:
private static bool IsPrime(int number)
{
for(int i = 2; i< number; i ++)
if(number%i == 0)return false;
返回true;
}
I am trying to combine prime numbers, even numbers and odd numbers and their results in a Windows Form Application. I have tested the code in Console but in Windows Form it will not loop to the next applicable number. For example: In console 1 - 10 in primes would result in "2, 3, 5, 7", however in Windows Form Application it will result in "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;
}
}
}
}
}
}
解决方案 I think LINQ is more suitable here, You can try this:
int[] numbers = Enumerable.Range(f, i-f).ToArray<int>();
string oddNumbers=string.Join(",", from number in numbers
where (number % 2)!=0
select number);
string evenNumbers = string.Join(",", from number in numbers
where (number % 2) == 0
select number);
Result.Text = "The Even Numbers Are:" + evenNumbers;
Result.Text = "The Odd Numbers Are:" + oddNumbers;
Updates for Prime number:
var primeNumbers= string.Join(",",from number in numbers
where (IsPrime(number))
select number);
Where IsPrime()
method is defined as follows?
private static bool IsPrime(int number)
{
for (int i = 2; i < number; i ++)
if (number % i == 0) return false;
return true;
}
这篇关于为什么循环不会在我的Windows窗体应用程序中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-24 05:53