有没有比使用这些if语句更有效的编码方式?

时间:2013-02-06 18:52:57

标签: c# if-statement

对于我的部分代码,有一节是相同的if if一遍又一遍,唯一的区别是其中一个变量。有没有更好的方法来构建我的代码?

if (buttonArray[m, j].BackColor == levelTwo && buttonArray[m, a].BackColor == levelTwo
 && buttonArray[i, j].BackColor == levelTwo)

{
    buttonArray[i, j].BackColor = levelThree;
    buttonArray[m, j].BackColor = Color.Transparent;
    buttonArray[m, a].BackColor = Color.Transparent;
}

if (buttonArray[m, j].BackColor == levelThree && buttonArray[m, a].BackColor == levelThree
 && buttonArray[i, j].BackColor == levelThree)
{
    buttonArray[i, j].BackColor = levelFour;
    buttonArray[m, j].BackColor = Color.Transparent;
    buttonArray[m, a].BackColor = Color.Transparent;
}

if (buttonArray[m, j].BackColor == levelFour && buttonArray[m, a].BackColor == levelFour
 && buttonArray[i, j].BackColor == levelFour)
{
    buttonArray[i, j].BackColor = levelFive;
    buttonArray[m, j].BackColor = Color.Transparent;
    buttonArray[m, a].BackColor = Color.Transparent;
}

if (buttonArray[m, j].BackColor == levelFive && buttonArray[m, a].BackColor == levelFive
 && buttonArray[i, j].BackColor == levelFive)
{
    buttonArray[i, j].BackColor = levelSix;
    buttonArray[m, j].BackColor = Color.Transparent;
    buttonArray[m, a].BackColor = Color.Transparent;
}

if (buttonArray[m, j].BackColor == levelSix && buttonArray[m, a].BackColor == levelSix
 && buttonArray[i, j].BackColor == levelSix)
{
    buttonArray[i, j].BackColor = levelSeven;
    buttonArray[m, j].BackColor = Color.Transparent;
    buttonArray[m, a].BackColor = Color.Transparent;
}

if (buttonArray[m, j].BackColor == levelSeven && buttonArray[m, a].BackColor == levelSeven && buttonArray[i, j].BackColor == levelSeven)
{
    buttonArray[i, j].BackColor = levelEight;
    buttonArray[m, j].BackColor = Color.Transparent;
    buttonArray[m, a].BackColor = Color.Transparent;
}

3 个答案:

答案 0 :(得分:4)

你可以创建一个方法(我不知道Level的类型,但是......)

private void CheckLevel(int levelIndex)
{
    if (buttonArray[m, j].BackColor == levelArray[levelIndex] && buttonArray[m, a].BackColor == levelArray[levelIndex] 
        && buttonArray[i, j].BackColor == levelArray[levelIndex])
    {
        buttonArray[i, j].BackColor = levelArray[levelIndex + 1];
        buttonArray[m, j].BackColor = Color.Transparent;
        buttonArray[m, a].BackColor = Color.Transparent;
    }

}

并在您的代码中:

CheckLevel(LevelTwoIndex);
CheckLevel(LevelThreeIndex);
CheckLevel(LevelFourIndex);

答案 1 :(得分:2)

看起来你想要做的就是每次发生事情时都循环所有这些按钮的颜色。

这是对这个概念的更重要的重写:

首先创建一个private Queue<Color> colors = new Queue<Color>();这将包含您按照使用顺序循环的所有颜色。

我们可以在首次创建表单时填充它:

colors.Enqueue(Color.Red);
colors.Enqueue(Color.Yellow);
colors.Enqueue(Color.Violet);
//Add other colors

现在我们只需要一种方法将“下一个”颜色应用于任意数量的控件:

public void ApplyNextColor(params Control[] controls)
{
    Color nextColor = colors.Dequeue();
    colors.Enqueue(nextColor);//add to end so that we cycle; 
    //you can optionally remove and do nothing if there are not items.

    foreach (Control control in controls)
        control.BackColor = nextColor;
}

然后我们可以通过例如按钮点击事件或计时器刻度事件来调用它:

ApplyNextColor(buttonArray[m, j], buttonArray[m, a]);

答案 2 :(得分:0)

if (buttonArray[m, j].BackColor == buttonArray[m, a].BackColor 
        && buttonArray[m, a].BackColor == buttonArray[i, j].BackColor) {
    if (buttonArray[m, j].BackColor == levelThree) { // or a switch - case
        ....
    }
}