winforms 为什么循环在我的Windows窗体应用程序中不起作用?[duplicate]

vx6bjr1n  于 2023-03-03  发布在  Windows
关注(0)|答案(3)|浏览(133)
    • 此问题在此处已有答案**:

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;
                }
            }
        }
    }
}
w80xi6nr

w80xi6nr1#

将代码更改为:

private void Primes_CheckedChanged(object sender, EventArgs e)
    {
        {
            string final = "The Prime Numbers Are:";// you need to keep the result out of the loop instead of reset it everytime
            int f = Convert.ToInt32(Min.Text);
            int i = Convert.ToInt32(Max.Text);
            bool isPrime = true;

            for (f = 0; f <= i; f++)// why set f=0 here ? Does not f = min already ?
            {
                for (int j = 2; j <= i; j++)// maybe j < f not j <= i
                {

                    if (f != j && f % j == 0)// then remove f != j here
                    {
                        isPrime = false;
                        break;
                    }

                }
                if (isPrime)
                    final = final + " " + f;// then add your found number to the result here
                isPrime = true;
            }
            Result.Text = final;
        }
    }

偶数和奇数是一样的。顺便说一句,1不是质数,对吗?

e37o9pze

e37o9pze2#

我会您的循环/检查组合成一个方法,如下所示:

private void Form1_Load(object sender, EventArgs e)
    {
        this.Primes.CheckedChanged += Options_CheckedChanged;
        this.Evens.CheckedChanged += Options_CheckedChanged;
        this.Odds.CheckedChanged += Options_CheckedChanged;
        this.Min.TextChanged += Range_Changed;
        this.Max.TextChanged += Range_Changed;
        CheckNumbers();
    }

    private void Range_Changed(object sender, EventArgs e)
    {
        CheckNumbers();
    }

    private void Options_CheckedChanged(object sender, EventArgs e)
    {
        CheckNumbers();
    }

    private void CheckNumbers()
    {
        int min, max;
        try
        {
            min = Convert.ToInt32(Min.Text);
            max = Convert.ToInt32(Max.Text);
        }
        catch (Exception)
        {
            Results.Text = "Invalid Range!";
            return;
        }

        List<int> lstPrimes = new List<int>();
        List<int> lstEvens = new List<int>();
        List<int> lstOdds = new List<int>();

        if (Primes.Checked || Evens.Checked || Odds.Checked)
        {
            bool isPrime;
            for (int f = min; f <= max; f++)
            {
                if (Primes.Checked)
                {
                    isPrime = true;
                    for (int j = 2; j <= max; j++)
                    {

                        if (f != j && f % j == 0)
                        {
                            isPrime = false;
                            break;
                        }

                    }
                    if (isPrime)
                    {
                        lstPrimes.Add(f);
                    }
                }
                int modResult = f % 2;
                if (Evens.Checked && modResult == 0)
                {
                    lstEvens.Add(f);
                }
                if (Odds.Checked && modResult != 0)
                {
                    lstOdds.Add(f);
                }
            }
        }

        StringBuilder sb = new StringBuilder();
        if (Primes.Checked)
        {
            sb.AppendLine("The Prime Numbers Are:" + String.Join(",", lstPrimes));
        }
        if (Evens.Checked)
        {
            sb.AppendLine("The Even Numbers Are:" + String.Join(",", lstEvens));
        }
        if (Odds.Checked)
        {
            sb.AppendLine("The Odd Numbers Are:" + String.Join(",", lstOdds));
        }
        Results.Text = sb.ToString();
    }
vsdwdz23

vsdwdz233#

我认为LINQ更适合这里,你可以试试这个:

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;

Prime编号更新:

var primeNumbers= string.Join(",",from number in numbers
                                 where (IsPrime(number))
                                 select number);

其中IsPrime()方法定义如下?

private static bool IsPrime(int number)
 {
   for (int i = 2; i < number; i ++)
     if (number % i == 0) return false;
   return true;
 }

相关问题