1

该应用程序必须显示五个城市的温度。我无法显示最低和最高温度列。我将温度读数的前三列与平均温度列一起显示,但无法显示第四列温度读数。另外,我无法获得面积平均值标签来显示面积平均值。有任何想法吗?

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]);
        }
    }
  }
}
4

0 回答 0