我正在编写一个简单的水果机,这是我的方法之一,我想知道是否可以使这段代码更高效:(在使用案例之前,我有一个 if / else if 语句)
_intNudgeCount 得到一个介于 0 - 9 之间的数字,因此是这种情况。
public void DrawNudgeCount()
{
switch (_intNudgeCount)
{
case 9:
pictureBoxNudgeCount.Image = Properties.Resources._9;
break;
case 8:
pictureBoxNudgeCount.Image = Properties.Resources._8;
break;
case 7:
pictureBoxNudgeCount.Image = Properties.Resources._7;
break;
case 6:
pictureBoxNudgeCount.Image = Properties.Resources._6;
break;
case 5:
pictureBoxNudgeCount.Image = Properties.Resources._5;
break;
case 4:
pictureBoxNudgeCount.Image = Properties.Resources._4;
break;
case 3:
pictureBoxNudgeCount.Image = Properties.Resources._3;
break;
case 2:
pictureBoxNudgeCount.Image = Properties.Resources._2;
break;
case 1:
pictureBoxNudgeCount.Image = Properties.Resources._1;
break;
case 0:
pictureBoxNudgeCount.Image = Properties.Resources._0;
break;
}
}
提前致谢!
解决了:
没关系,我把它简化为 3 行代码:
//在类的顶部声明资源图像。
private System.Drawing.Image[] _arrayNudgeCount;
//加载类时填充数组。
_arrayNudgeCount = new System.Drawing.Image[] { Properties.Resources._0, Properties.Resources._1};
//重绘图片
public void DrawNudgeCount()
{
pictureBoxNudgeCount.Image = _arrayNudgeCount[_intNudgeCount];
}