2

考虑代码:

private String mode;        // current played mode

private void func(int row , int column)
{
    // rotation mode 
    if ((row == 0 && column == 0) || (row == 2 && column == 0) || (row == 0 && column == 2) || (row == 2 && column == 2)) 
    {
        mode = ROTATE_MODE;
    }

    // scaling more 
    else if ((row == 0 && column == 1) || (row == 1 && column == 0) || (row == 2 && column == 1) || (row == 1 && column == 2)) 
    {
        mode = SCALE_MODE;
    }

    // translate mode
    else if ((row == 1 && column == 1)) 
    {
        mode = TRANSLATE_MODE;
    }
}

如何将 Switch-case 用于rowand column

4

2 回答 2

6

根据值的范围,您可以使用公式

switch(row * 10 + column) {
   case 0, 20, 2, 22:

       break;
   case 1, 10, 21, 12:

       break;
   case 11:

       break;
 }
于 2013-04-27T14:23:49.763 回答
3

您可以将它们组合成一个变量。例如,如果最大列数为 100 (0-99):

int position = row * 100 + column;
switch (position) {
  case 0: // row 0, col 0
  case 200: // row 2, col 0
  case 2: // row 0, col 2
  case 202: // row 2, col 2
    this.m_mode = ROTATE_MODE;
    break;
  ...
}
于 2013-04-27T14:29:26.253 回答