0

我有以下代码。一个 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);

最后一行给了我一个错误,说明该变量在当前上下文中不存在。有任何想法吗?

4

5 回答 5

7

编译器要求您确保stateab在读取变量之前为其分配了一些值。在您的情况下,您需要满足值不满足任何case子句的情况s(即使您的应用程序的逻辑不可能做到这一点)。

您可以通过stateab使用初始值初始化变量来轻松解决它:

string stateab = "";

……或者default在你的陈述中加入一个子句switch

case "Wyoming":
    stateab = "WY";
    break;
default:
    stateab = "Unknown";
    break;
于 2012-06-09T17:09:45.870 回答
2

您需要break;而不是在每个案例结束时继续..

于 2012-06-09T17:06:18.360 回答
0

C#在使用 switch 语句时需要默认大小写。没有它就无法编译。最后值得尝试默认情况。

default:
   //do something
   break;
于 2012-06-09T17:09:35.507 回答
0

这是switch声明的格式:

switch (expression) {    
      case constant-expression: statement
      jump-statement  
      [default:statement
      jump-statement] 
}

您应该包括一个default案例并break在每个案例的末尾使用。

于 2012-06-09T17:09:52.460 回答
0

你有一个编译器错误,因为局部变量可能保持未初始化,如果你将switch一些字符串传递给你的语句,这些字符串不会在案例中列出(例如"Hello world")。因此,您应该在第一次使用本地变量之前对其进行初始化。您可以在声明之前的声明点执行此操作switch

string stateab = "";

或者,您可以将default大小写添加到您switch引发异常的位置(或提供默认值以缩写)。

switch (s)
{
    case "Alabama":
         stateab = "AL";
         break;
    // ...
    default:
         throw new ArgumentOutOfRange("s");
}

.

于 2012-06-09T17:11:02.547 回答