【发布时间】:2017-01-21 21:14:00
【问题描述】:
下面的方法打印这个:
Minimum Temperature is 16
Maximum Temperature is 27
The Average Temperature is 22
现在,除了温度之外,我还想要温度最高和最低的日子,例如:
Minimum Temperature is 16 on Day 6
Maximum Temperature is 27 on Day 8
The Average Temperature is 22
这是将 Day 和 Temperature 作为字典参数插入到数组中并将它们传递给确定最小值、最大值、平均值的方法的方法。
最小值和最大值是字典的int值,我的问题是我们如何根据这些值确定相关的字符串日期?
// if user select January , the January() execute:
protected void Button1_Click(object sender, EventArgs e)
{
// assigning Days and Temperatures to Dictionary and making array dictionary
Dictionary<string, int>[] temperatures = new Dictionary<string, int>[10];
temperatures[0] = new Dictionary<string, int>();
temperatures[1] = new Dictionary<string, int>();
temperatures[2] = new Dictionary<string, int>();
temperatures[3] = new Dictionary<string, int>();
temperatures[4] = new Dictionary<string, int>();
temperatures[5] = new Dictionary<string, int>();
temperatures[6] = new Dictionary<string, int>();
temperatures[7] = new Dictionary<string, int>();
temperatures[8] = new Dictionary<string, int>();
temperatures[9] = new Dictionary<string, int>();
temperatures[0].Add("Day1", 22);
temperatures[1].Add("Day2", 23);
temperatures[2].Add("Day3", 25);
temperatures[3].Add("Day4", 26);
temperatures[4].Add("Day5", 18);
temperatures[5].Add("Day6", 16);
temperatures[6].Add("Day7", 17);
temperatures[7].Add("Day8", 27);
temperatures[8].Add("Day9", 23);
temperatures[9].Add("Day10", 24);
if (DropDownList1.SelectedValue.ToString() == "January")
{
January(temperatures);
}
//the metthod which calculate min ,max and ..
private void January(Dictionary<string, int>[] temperatures)
{
int Minimumtemperture = 40;
int Maximumtemperture = 0;
int total = 0;
int averageTemperatures = 0;
// this foreach goes through array
foreach (var temperture in temperatures)
{
// this foreach goes throuh dictionary
foreach (var degree in temperture)
{
//assigning value of each dictionary to the monthTemp
int MonthTemps = degree.Value;
if (MonthTemps < Minimumtemperture)
{
Minimumtemperture = MonthTemps;
}
if (MonthTemps>Maximumtemperture)
{
Maximumtemperture = MonthTemps;
}
total = total + MonthTemps;
}
int totaltemperature = temperatures.Length;
averageTemperatures = (total / totaltemperature);
}
// printing the result
Label1.Text = string.Format("Minimum Temperature is {0}<br/> Maximum Temperature is{1}<br/> The Average Temperature is{2}<br/>", Minimumtemperture, Maximumtemperture, averageTemperatures);
}
【问题讨论】:
-
字典数组似乎有点不寻常。每个月都有单独的字典吗?
-
可能是的,我这样做只是为了尝试数组的不同方面,我也对枚举做了同样的事情,我想知道在这种情况下哪个更好?可能从数据库中获取信息是更好的选择