今天有人问我如何将下面的if语句转换为switch:
int i=5;
if(i>10)
{
Do Something;
}
else
{
Do Something else;
}
我建议假设 i 是一个只有正值的整数:
int i=5;
switch(i)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10: Do Something else;
break;
case default: Do Something;
break;
}
还有其他更优雅的方法吗?