如何使用两个变量的开关盒?

时间:2013-04-27 14:20:10

标签: java switch-statement

考虑代码:

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;
    }
}

如何为rowcolumn

使用Switch-case

2 个答案:

答案 0 :(得分:6)

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

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

       break;
   case 1, 10, 21, 12:

       break;
   case 11:

       break;
 }

答案 1 :(得分: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;
  ...
}