在以下代码中简化 Switch 语句

Simplify Switch Statement in following code

我怎样才能让这段代码更小更高效:) ..?。 到目前为止,我已经从一个更大的旧代码中做到了,但我认为它仍然很大。

if (affix == Mod.Affix)
{
    Graphics.DrawText(text, textSize, position, Color.White);

    switch (levels)
    {
        case 1:
        {
            Size level = Graphics.DrawText(text, textSize, position, Color.Yellow);
            if (level != new Size()) 
            { 
                position.Y += level.Height; 
            }
        } break;

        case 2:
        {
            Size level = Graphics.DrawText(text, textSize, position, Color.Red);
            if (level != new Size()) 
            {
                position.Y += level.Height; 
            }
        } break;

        case 3:
        {
            Size level = Graphics.DrawText(text, textSize, position, Color.Green);
            if (level != new Size()) 
            { 
                position.Y += level.Height; 
            }
        } break;

        default:
            Size nextLevel = Graphics.DrawText(text, textSize, position, Color.Black);

            if (nextLevel != new Size()) 
            { 
                position.Y += nextLevel.Height;
            } 
        break;
    }
}

提前致谢!

尝试这样的事情:

Color color = new Color();
switch (levels)
{
    case 1:
        color = Color.Yellow;
        break;

    case 2:
        color = Color.Red;
        break;

    case 3:
        color = Color.Green;
        break;

    default:
        color = Color.Black;
        break;
}
Size level = Graphics.DrawText(text, textSize, position, color);
if (level != new Size()) // ???
{ 
    position.Y += level.Height;
}

您正在为每种颜色执行相同的代码,试试这个:

switch (levels)
{
    case 1:
        AddHeight(Color.Yellow);
        break;
    case 2:
        AddHeight(Color.Red);
        break;
    case 3:
        AddHeight(Color.Green);
        break;
    default:
        AddHeight(Color.Black);
        break;
}

public void AddHeight(Color color){
    Size level = Graphics.DrawText(text, textSize, position, color);
    if (level != new Size()) // ???
    { 
        position.Y += level.Height;
    }
}

使用字典将级别映射到颜色:

private static Dictionary<int, Color> levelColors = new Dictionary<int, Color>
{
    { 1, Color.Yellow },
    { 2, Color.Red },
    { 3, Color.Green }
};

那么你可以把你的方法改成这样:

Color color;
if (!levelColors.TryGetValue(levels, out color)) // try and get the color for the level
{
    color = Color.Black; // Default to black if no level color found
}

Size level = Graphics.DrawText(text, textSize, position, color);
if (level != new Size())
{
    position.Y += level.Height;
}

这样一来,您 add/alter 级别颜色时无需修改方法,只需更新字典即可。

if (affix == Mod.Affix)
{
    Graphics.DrawText(text, textSize, position, Color.White);

    Size level =
         (levels == 1) ? Graphics.DrawText(text, textSize, position, Color.Yellow) 
         :((levels == 2) ? Graphics.DrawText(text, textSize, position, Color.Red) 
         : ((levels == 3) ? Graphics.DrawText(text, textSize, position, Color.Green) 
         : Graphics.DrawText(text, textSize, position, Color.Black)));

    if (level != new Size()) 
    { 
        position.Y += nextLevel.Height;
    }
}

您可以有一个预定义的映射 "level_to_color",例如(例如在静态构造函数中初始化):

Dictionary<int,Color> _levelToColor = new Dictionary<int,Color>();
_levelToColor.Add(1, Color.Yellow);
_levelToColor.Add(2, Color.Red);
_levelToColor.Add(3, Color.Green);

然后您的代码可能如下所示:

Color color = _levelToColor.ContainsKey( level ) ?  _levelToColor[level] : Color.Black;
Size level = Graphics.DrawText(text, textSize, position, color);
position.Y += level.Height;