C# 天气追踪应用
该应用程序必须显示五个城市的温度。我无法显示最低和最高温度列。我得到了要显示的前三列温度读数和平均温度列,但没有得到要显示的第四列温度读数。此外,我无法使用面积平均值标签来显示面积平均值。有什么想法吗C# 天气追踪应用,c#,C#,该应用程序必须显示五个城市的温度。我无法显示最低和最高温度列。我得到了要显示的前三列温度读数和平均温度列,但没有得到要显示的第四列温度读数。此外,我无法使用面积平均值标签来显示面积平均值。有什么想法吗 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Lab8practice1
{
public partial class Form1 : Form
{
string[] strCities = { "Troy",
"West Bloomfield",
"Farmington Hills",
"Brighton",
"Canton"};
int[,] intTemperatures = new int[5, 3];
public Form1()
{
InitializeComponent();
}
private void btnAddTemperature_Click(object sender, EventArgs e)
{
try
{
if (cmbCities.SelectedIndex >= 0 && cmbCities.SelectedIndex <= 4)
{
if (intTemperatures[cmbCities.SelectedIndex, (int)nudReading.Value -1] == 0)
{
intTemperatures[cmbCities.SelectedIndex, (int)nudReading.Value - 1] = Int32.Parse(txtTemperature.Text);
}
else
{
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
result = MessageBox.Show("Modify Temperature?", "Temperature already exists!", buttons);
if (result == System.Windows.Forms.DialogResult.Yes)
{
intTemperatures[cmbCities.SelectedIndex, (int)nudReading.Value - 1] = Int32.Parse(txtTemperature.Text);
}
}
displayTemperatures();
}
else
{
MessageBox.Show("Select a city!");
}
}
catch (FormatException)
{
MessageBox.Show("Temperatures must be whole numbers!");
}
}
private void displayTemperatures()
{
string strCities;
lstTemperatures.Items.Clear();
double[] dblAverages = new double[5];
int intNonBlank = 0;
double dblNonBlank = 0.0;
int intMin = 150, intMax = 0, intTotal = 0;
for (int g = 0; g <= 4; g++)
{
intNonBlank = 0;
for (int d = 0; d <= 2; d++)
{
if (intTemperatures[g, d] != 0)
{
dblAverages[g] += intTemperatures[g, d];
intNonBlank++;
}
}
if (intNonBlank != 0)
{
dblAverages[g] /= intNonBlank;
}
}
for (int g = 0; g <= 4; g++)
{
for (int d = 0; d <= 2; d++)
{
if (intTemperatures[g, d] != 0)
{
intTotal += intTemperatures[g, d];
dblNonBlank++;
}
if (intTemperatures[g, d] < intMin && intTemperatures[g, d] != 0)
{
intMin = intTemperatures[g, d];
}
if (intTemperatures[g, d] > intMax)
{
intMax = intTemperatures[g, d];
}
}
}
for (int g = 0; g <= 4; g++)
{
strCities = " ";
for (int d = 0; d <= 2; d++)
{
if (intTemperatures[g, d] == 0)
{
strCities += " ";
}
else
{
if (intTemperatures[g, d] == 103)
{
strCities += intTemperatures[g, d] + " ";
}
else
{
strCities += intTemperatures[g, d] + " ";
}
}
}
if (dblAverages[g] == 0)
{
strCities += " ";
}
else
{
if (dblAverages[g] == 140)
{
strCities += dblAverages[g].ToString("f2") + " ";
}
else
{
strCities += dblAverages[g].ToString("f2") + " ";
}
}
lstTemperatures.Items.Add(strCities);
lblAreaAverage.Text = dblAverages.ToString("f2");
}
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i <= 4; i++)
{
cmbCities.Items.Add(strCities[i]);
lstCities.Items.Add(strCities[i]);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间Lab8practice1
{
公共部分类Form1:Form
{
字符串[]strCities={“特洛伊”,
“西布隆菲尔德”,
“法明顿山”,
“布莱顿”,
“州”};
int[,]int温度=新的int[5,3];
公共表格1()
{
初始化组件();
}
私有无效btnAddTemperature_单击(对象发送者,事件参数e)
{
尝试
{
如果(cmbCities.SelectedIndex>=0&&cmbCities.SelectedIndex,因此您无法计算平均值?您可以通过屏幕截图复制此结果的输出吗?这可能也是学习如何使用调试器单步执行代码的好机会。for循环跳过最后一个条目:(inti=0;为什么有这么多循环?你没有想过OOP!