C#Weather程序,如何从循环中检索特定值

时间:2014-03-29 16:57:53

标签: c# visual-studio-2010 visual-studio for-loop weather

我正在制作一个天气程序,随机温度从-10到50°C。我有它工作但我需要找到有霜的特定日子(在0; -1,-5等等)。我有一个柜台,有多少霜冻天,但我似乎无法找到如何获得这些特定的日子,并在屏幕上打印它们。这是我的代码:

Random x = new Random();
int day = 1;
int frostDays = 0;
double sum = 0;
int[] days = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100};

int[] heat = new int[100];

for (int i = 1; i <= 100; i++)
{
    heat[0] = x.Next(-10, 51);

    if (heat[0] < 0)
    {
        //Console.WriteLine("Day number " + day + " heat " + heat[0]);
        frostDays++;
    }
    Console.Write(heat[0] + " ** ");
    if (days.Contains(day))
    {
        Console.WriteLine("\n");
    }
    //Console.WriteLine("Heat day " + day + ". is: " + heat[0]);

    sum += heat[0];
    day++;
}

double average = sum / 100;

Console.WriteLine("Heat sum: " + sum);
Console.WriteLine("average heat: " + average.ToString("F2") + " °C");
Console.WriteLine("frost days: " + frostDays);

输出:http://oi57.tinypic.com/mw4myv.jpg

我正在尝试用热量检索特定的日子(天数)(霜度在0°C以下)并将其打印在屏幕上,我似乎无法知道如果有人可以提供帮助,请回答,非常感谢。

1 个答案:

答案 0 :(得分:1)

我建议您将结构更改为更面向对象的方法。

您应该创建一个Day对象,其中包含:

  • 当天的温度
  • 如果这一天有霜冻

这可以用小班表示:

public class Day
{
    public int Temperature { get; set; }
    public bool Frost { get { return Temperature < 0; } }

    public Day(int temperature)
    {
        Temperature = temperature;
    }
}

现在你有了存储你的数据的东西,你需要处理程序的逻辑。 (这没有冒犯,结构不是很好)

首先给你的程序一些变量:

private const totalDays = 100; //Replace your hardcoded "100"s with this
private const int minTemp = -10; //Pretty self explanitory, the min and max temperatures for a day to generate
private const int maxTemp = 50;

因为您的问题表明您正在寻找霜冻天,我将修改您的逻辑以仅打印它们。另请注意,您使用heat[0]错误地访问了数组,这将始终获得0位置的第一个元素,当您应该使用heat[i]获取i处的值时{1}}位置。

代码应如下:

for (int i = 0; i < totalDays; i++)
{
    days[i] = new Day(x.Next(minTemp, maxTemp + 1)); //Create a day with random temperature
    if (days[i].Frost) //The "Frost" propery returns true if the "Temperature" is below 0
    {
         Console.WriteLine("Day {0} is frosty!", i);
    }
}

注意我是如何将for循环修改为从0而不是1开始的,因为这是数组的开头。我还更改了x.Next以考虑新的minTempmaxTemp值。


如果您想获取并处理霜冻物品,您只需循环浏览它们并检查Day Frost属性是否为真。

回顾一下,您的代码应如下所示:

class Program
{
    private const int totalDays = 100;
    private const int minTemp = -10;
    private const int maxTemp = 50;
    private static Day[] days = new Day[totalDays];
    private static Random x = new Random();

    static void Main(string[] args)
    {
        double sum = 0;

        for (int i = 0; i < totalDays; i++)
        {
            days[i] = new Day(x.Next(minTemp, maxTemp + 1)); //Create a day with random temperature
            sum += days[i].Temperature;
            Console.WriteLine("Day {0} is {1} degrees and is{2} frosty", i, days[i].Temperature, days[i].Frost ? string.Empty : " not");
        }

        double average = sum / totalDays;

        Console.WriteLine("Total Temperature: {0} °C", sum);
        Console.WriteLine("Average Temperature {0} °C", average.ToString("F2"));

        for (int i = 0; i < totalDays; i++)
        {
            if (days[i].Frost)
            {
                //Do something
            }
        }

        Console.ReadLine();
    }
}
public class Day
{
    public int Temperature { get; set; }
    public bool Frost { get { return Temperature < 0; } }

    public Day(int temperature)
    {
        Temperature = temperature;
    }
}