什么是使用嵌套switch / case语句的最佳/最简单的方法是什么? [英] What is the best/easiest way to use nested switch/case statements?
本文介绍了什么是使用嵌套switch / case语句的最佳/最简单的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是以下两个开关/ case语句的更好的做法?
有没有更简单的方法(低于code)来做到这一点?
开关(myvalue的)
{
情况1:
{
methodFor1();
打破;
}
案例2:
案例3:
{
methodFor2or3();
如果(myvalue的== 2)
methodFor2();
如果(myvalue的== 3)
methodFor3();
打破;
}
}
...要么...
开关(myvalue的)
{
情况1:
{
methodFor1();
打破;
}
案例2:
案例3:
{
methodFor2or3();
开关(myvalue的)
{
案例2:
{
methodFor2();
打破;
}
案例3:
{
methodFor3();
打破;
}
}
打破;
}
}
解决方案
开关(myvalue的)
{
情况1:
methodFor1();
打破;
案例2:
methodFor2or3();
methodFor2();
打破;
案例3:
methodFor2or3();
methodFor3();
打破;
}
为什么所有的麻烦只是为了避免重复 methodFor2or3()
的一次的?
What is the better practice of the following two switch/case statements?
Is there an easier way (less code) to do this?
switch (myValue)
{
case 1:
{
methodFor1();
break;
}
case 2:
case 3:
{
methodFor2or3();
if (myValue == 2)
methodFor2();
if (myValue == 3)
methodFor3();
break;
}
}
...or...
switch (myValue)
{
case 1:
{
methodFor1();
break;
}
case 2:
case 3:
{
methodFor2or3();
switch (myValue)
{
case 2:
{
methodFor2();
break;
}
case 3:
{
methodFor3();
break;
}
}
break;
}
}
解决方案
switch (myValue)
{
case 1:
methodFor1();
break;
case 2:
methodFor2or3();
methodFor2();
break;
case 3:
methodFor2or3();
methodFor3();
break;
}
Why all the hassle just to avoid repeating methodFor2or3()
once?
这篇关于什么是使用嵌套switch / case语句的最佳/最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文