"无法通过"切换错误

时间:2015-08-30 19:48:50

标签: c# switch-statement

} else {
    int indexGenerator = numberGenerator.Next(1, 4);
    int answer1 = finalUserInput - theCorrectAnswer;
    switch (indexGenerator) {
        case 1:
            Console.WriteLine("You are off by" + answer1);
        case 2:
            Console.WriteLine("So close. Maybe if you add or subtract " + answer1 + "?");

当我尝试让开关工作时,会弹出一条错误消息,提示控制案例无法从Case1转移到另一个。

3 个答案:

答案 0 :(得分:1)

您需要在switch语句的每个案例末尾添加break;

switch (indexGenerator) {
  case 1:
      Console.WriteLine("You are off by" + answer1);
      break; 
  case 2:
      Console.WriteLine("So close. Maybe if you add or subtract " + answer1 + "?");
      break;
}

答案 1 :(得分:0)

此处的问题是您忘记将$result= preg_split("/[^A-Za-z0-9]/", $data); 添加到您的代码中。您的代码应如下所示。

break;

拥有switch (indexGenerator){ case 1: Console.WriteLine("You are off by" + answer1); break; case 2: Console.WriteLine("So close. Maybe if you add or subtract " + answer1 + "?"); break; } 的目的是基本上告诉你的程序你正在脱离语句的break部分。

答案 2 :(得分:0)

false区块末尾需要break

case 1

在我显示的} else { int indexGenerator = numberGenerator.Next(1, 4); int answer1 = finalUserInput - theCorrectAnswer; switch (indexGenerator) { case 1: Console.WriteLine("You are off by" + answer1); break; // missing break case 2: Console.WriteLine("So close. Maybe if you add or subtract " + answer1 + "?"); break; // missing break 块的末尾添加一个。