循环计算生物体生长

时间:2016-03-10 01:08:58

标签: c#

我正在尝试制作一个循环来计算生物体的数量,但我仍然坚持如何进行循环更新。我是否需要在for循环之外放置一些东西来更新总生物体?

private void calculateButton_Click(object sender, EventArgs e)
    {
        //declare variables for number of days passed and population 
        double days;
        double organisms;
        double increaseDaily;
        double total_organisms;

        //declare the constants to be used 
        const int interval = 1;
        const int start_days = 1;

        //try parse to get amount of starting organisms
        if (double.TryParse(organismTextBox.Text, out organisms))
        {
            //try parse to get the percent daily increase
            if (double.TryParse(dailyIncreaseTextBox.Text, out  increaseDaily))
            {
                //try parse to get the number of days passed
                if (double.TryParse(daysMultiplyTextBox.Text, out days))
                {

                    //for loop to count through the number of days
                    for (int i = 1; i <= days; i += interval)
                    {

                        //calculate the amount of organisms
                        total_organisms = (organisms * (increaseDaily / 100) + organisms);

                        //display the amount of organisms after an amount of time
                        listBox1.Items.Add("after " + i + " days, the amount of organisms is " + total_organisms);
                    }

2 个答案:

答案 0 :(得分:0)

每个循环,您将total_organisms计算为organisms加上一些百分比的总和:

total_organisms = (organisms * (increaseDaily / 100) + organisms);

您永远不会更改organisms的值,因此total_organisms将被计算为每个循环的相同值。您应该更新organisms的值。

此外,您可以通过更改每个if语句来测试解析失败和纾困来减少代码中的缩进:

private void calculateButton_Click(object sender, EventArgs e)
{
    //declare variables for number of days passed and population 
    double days;
    double organisms;
    double increaseDaily;

    List<string> errors = new List<string>();
    //declare the constants to be used 
    const int interval = 1;
    const int start_days = 1;

    //try parse to get amount of starting organisms
    if (!double.TryParse(organismTextBox.Text, out organisms)) {
        errors.Add("Organisms must be a valid number");
    }
    //try parse to get the percent daily increase
    if (double.TryParse(dailyIncreaseTextBox.Text, out  increaseDaily)) {
        errors.Add("Daily increase must be a valid number");
    }
    //try parse to get the number of days passed
    if (double.TryParse(daysMultiplyTextBox.Text, out days)) {
        errors.Add("Number of days must be a valid number");
    }
    if (errors.Any()) {
        // Display errors to user here (depending on your UI)
        return;
    }
    //for loop to count through the number of days
    for (int i = 1; i <= days; i += interval) {
        //calculate the amount of organisms
        organisms = (organisms * (increaseDaily / 100) + organisms);

        //display the amount of organisms after an amount of time
        listBox1.Items.Add(
            "after " + i + " days, the amount of organisms is " + organisms);
   }
}

答案 1 :(得分:0)

如果你想继续增加有机体的总数:

total_organisms = total_organisms + (organisms * (increaseDaily / 100) + organisms);