如何在 switch 语句中使用变量

How to use a variable in switch statement

在 "switch %direction%" 上出现错误 - 此行包含无法识别的操作。 为什么错了?

myFunction(direction)
{
   switch %direction%
   {
      case "left":
      break
      case "right":
      break
   }
   return
}

我真的不知道你用的是什么语言,但你能试试这个吗:

myFunction(direction)
{
   switch direction
   {
      case "left":
      break
      case "right":
      break
   }
   return

更新: Switch 需要版本 1.1.31+


"Break" 不用于 AHK switch 语句。 (Documentation)

The first statement of each case may be below Case or on the same line, following the colon. Each case implicitly ends at the next Case/Default or the closing brace. Unlike the switch statement found in some other languages, there is no implicit fall-through and Break is not used (except to break out of an enclosing loop).

这应该有效:

myFunction(direction)
{
   switch direction
   {
      case "left": MsgBox "LEFT"      
      case "right": MsgBox "RIGHT"      
   }
   return
}