while循环,windows形成运动c#

时间:2016-02-07 20:52:21

标签: c#

更新 感谢您的答复。非常感激。我为我的菜鸟状态道歉。这是我在c#中的第一个作业,也是我的第一个c#类。没有太多以前的编程经验。所以很多这些概念都是首次引入的。这就是我完成作业第一部分所做的工作。

    public partial class Form1 : Form
{
    public bool oddNum(double value)
    {
        return value % 2 != 0;
    }
    public bool IsPrime(int n)
    {
        if (n > 1)
        {
            return Enumerable.Range(1, n).Where(x => n % x == 0).SequenceEqual(new[] { 1, n });
        }
        return false;
    }
    public bool evenNum(double value)
    {
        return value % 2 == 0;
    }
    public Form1()
    {
        InitializeComponent();
    }

    private void btnGO_Click(object sender, EventArgs e)
    {
        double start = Double.Parse(txtStart.Text);
        double end = Double.Parse(txtEnd.Text);
        double oddCount = 0;
        double evenCount = 0;
        double evenSum = 0;
        double oddSum = 0;
        double totalSum = 0;
        double avrgNum = 0;
        double avrgSum = 0;
        double productNum = 1;         



        while (start <= end)
        {

            if (oddNum(start))
            {
                oddSum += start;
                oddCount++;
            }
            else
            {
                evenSum += start;
                evenCount++;
            }
            totalSum += start;
            productNum *= start;
            avrgNum = totalSum / end;
            avrgSum = avrgNum / totalSum;
            start++;
         }
        string info = ("Number of odd numbers: " + oddCount.ToString() + "\n" + "Number of even numbers: " + evenCount.ToString() + "\n" + "Sum of odd numbers: " + oddSum.ToString() + "\n" +
                        "Sum of even numbers: " + evenSum.ToString() + "\n" + "Sum of all numbers: " + totalSum.ToString() + "\n" + "Product of all numbers: " + productNum.ToString() + "\n" +
                         "Average: " + avrgNum.ToString("F2") + "\n" + "Average / Sum of all numbers: " + avrgSum.ToString("F2"));
        lblOutput.Text = info;


    }

    private void btnPrime_Click(object sender, EventArgs e)
    {
        int start = Convert.ToInt32(txtStart.Text);
        if (IsPrime(start))
        {
            MessageBox.Show("The start number is prime.");
        }
        else
        {
            MessageBox.Show("The start number is not prime.");
        }
    }
 }

}

____________

“创建一个执行一些while循环的Windows窗体应用程序,if-else语句和double 类型计算。“

1 个答案:

答案 0 :(得分:0)

当你循环遍历while循环时,你应该通过调用你的一个方法来检查数字是奇数还是偶数(实际上不需要两者)。像这样:

while (startingNumber != endingNumber)
{
    if (oddNum(startingNumber))
        oddNumbersLabel.Text += " " + startingNumber;
    else
        evenNumbersLabel.Text += " " + startingNumber;

    startingNumber += incrementValue;
}