samedi 8 février 2020

C# - Convert Switch statement to If-Else

I'm having a bit of trouble getting going on this particular assignment. Taking a switch statement and converting it to if-else. This program utilizes a list box to select the location and show the corresponding time zone.

if (cityListBox.SelectedIndex != -1)
        {
            //Get the selected item.
            city = cityListBox.SelectedItem.ToString();

            // Determine the time zone.
            switch (city)
            {
                case "Honolulu":
                    timeZoneLabel.Text = "Hawaii-Aleutian";
                    break;
                case "San Francisco":
                    timeZoneLabel.Text = "Pacific";
                    break;
                case "Denver":
                    timeZoneLabel.Text = "Mountain";
                    break;
                case "Minneapolis":
                    timeZoneLabel.Text = "Central";
                    break;
                case "New York":
                    timeZoneLabel.Text = "Eastern";
                    break;
            }
        }
        else
        {
            // No city was selected.
            MessageBox.Show("Select a city.");

Aucun commentaire:

Enregistrer un commentaire