我有以下代码。一个 switch 语句,它接受一个状态名称并将另一个变量设置为该状态的缩写。但是,在我关闭我的 switch 语句后,我收到一条错误消息,指出该变量无法访问。
string stateab;
switch (s)
{
case "Alabama":
stateab = "AL";
break;
case "Alaska":
stateab = "AK";
break;
case "Arizona":
stateab = "AZ";
break;
case "Arkansas":
stateab = "AR";
break;
case "California":
stateab = "CA";
break;
case "Colorado":
stateab = "CO";
break;
case "Connecticut":
stateab = "CT";
break;
case "Delaware":
stateab = "DE";
break;
case "DENIED":
stateab = "DENIED";
break;
case "District of Columbia":
stateab = "DC";
break;
case "Florida":
stateab = "FL";
break;
case "Georgia":
stateab = "GA";
break;
case "Hawaii":
stateab = "HI";
break;
case "Idaho":
stateab = "ID";
break;
case "Illinois":
stateab = "IL";
break;
case "Indiana":
stateab = "IN";
break;
case "Iowa":
stateab = "IA";
break;
case "Kansas":
stateab = "KS";
break;
case "Kentucky":
stateab = "KY";
break;
case "Louisiana":
stateab = "LA";
break;
case "Maine":
stateab = "ME";
break;
case "Maryland":
stateab = "MD";
break;
case "Massachusetts":
stateab = "MA";
break;
case "Michigan":
stateab = "MI";
break;
case "Minnesota":
stateab = "MN";
break;
case "Mississippi":
stateab = "MS";
break;
case "Missouri":
stateab = "MO";
break;
case "Montana":
stateab = "MT";
break;
case "Nebraska":
stateab = "NE";
break;
case "Nevada":
stateab = "NV";
break;
case "New Hampshire":
stateab = "NH";
break;
case "New Jersey":
stateab = "NJ";
break;
case "New York":
stateab = "NY";
break;
case "North Carolina":
stateab = "NC";
break;
case "North Dakota":
stateab = "ND";
break;
case "Ohio":
stateab = "OH";
break;
case "Oklahoma":
stateab = "OK";
break;
case "Oregon":
stateab = "OR";
break;
case "Pennsylvania":
stateab = "PA";
break;
case "Rhode Island":
stateab = "RI";
break;
case "South Carolina":
stateab = "SC";
break;
case "South Dakota":
stateab = "SD";
break;
case "Tennessee":
stateab = "TN";
break;
case "Texas":
stateab = "TX";
break;
case "Utah":
stateab = "UT";
break;
case "Vermont":
stateab = "VT";
break;
case "Virginia":
stateab = "VA";
break;
case "Washington":
stateab = "WA";
break;
case "West Virginia":
stateab = "WV";
break;
case "Wisconsin":
stateab = "WI";
break;
case "Wyoming":
stateab = "WY";
break;
}
Console.WriteLine(stateab);
最后一行给了我一个错误,说明该变量在当前上下文中不存在。有任何想法吗?