【发布时间】:2020-02-08 20:22:56
【问题描述】:
我在执行这项特定任务时遇到了一些麻烦。采用 switch 语句并将其转换为 if-else。该程序利用列表框来选择位置并显示相应的时区。
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.");
【问题讨论】:
-
到目前为止您尝试过什么?
switch有什么问题?
标签: c# if-statement listbox switch-statement