在循环中,Windows 形成练习 C#

本文关键字:练习 Windows 循环 | 更新日期: 2023-09-27 18:33:32

更新感谢您的回复。非常感谢。我为我的菜鸟身份道歉。这是我第一次学习 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.");
        }
    }
 }

}

____

______

__

"创建一个 Windows 窗体应用程序,该应用程序执行一些 while 循环、if-else 语句和双精度类型计算。

在循环中,Windows 形成练习 C#

当你循环

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

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