我已经做了一段时间了但是却无法弄明白。基本上它是一个带有多个单选按钮的混色程序,问题是当选择blue1和blue 2时,formbackground应该变为蓝色。由于下面的代码,它变为紫色。无论如何通过像try / finally,OrElse语句那样优先处理代码吗?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void mixrButton_Click(object sender, EventArgs e)
{
if (red1.Checked && red2.Checked)
{
this.BackColor = Color.Red;
}
if (blue1.Checked && blue2.Checked)
{
this.BackColor = Color.Blue;
}
if (yellow1.Checked && yellow2.Checked)
{
this.BackColor = Color.Yellow;
}
if ((red1.Checked || blue1.Checked) && (red2.Checked || blue2.Checked))
{
this.BackColor = Color.Purple;
}
if ((red1.Checked || yellow1.Checked) && (red2.Checked || yellow2.Checked))
{
this.BackColor = Color.Orange;
}
if ((blue1.Checked || blue2.Checked) && (yellow1.Checked || yellow2.Checked))
{
this.BackColor = Color.Green;
}
}
private void clear_Click(object sender, EventArgs e)
{
this.BackColor = SystemColors.Control;
}
}
答案 0 :(得分:0)
您的主要问题是:
Purple
逻辑错误的事实在这里,即使您没有使用else
语句,也应该使用蓝色,但是您说它将是紫色if ((red1.Checked || blue1.Checked) && (red2.Checked || blue2.Checked))
,也就是说
以下是您想要的颜色的可能实现:
private void mixrButton_Click(object sender, EventArgs e)
{
if (red1.Checked && red2.Checked)
{
this.BackColor = Color.Red;
}
else if (blue1.Checked && blue2.Checked)
{
this.BackColor = Color.Blue;
}
else if (yellow1.Checked && yellow2.Checked)
{
this.BackColor = Color.Yellow;
}
else if ((red1.Checked && blue2.Checked) || (blue1.Checked && red2.Checked))
{
this.BackColor = Color.Purple;
}
else if ((red1.Checked && yellow2.Checked) || (yellow1.Checked && red2.Checked))
{
this.BackColor = Color.Orange;
}
else if ((blue1.Checked && yellow2.Checked) || (yellow1.Checked && blue2.Checked))
{
this.BackColor = Color.Green;
}
}
您还可以创建一个返回Backcolor的函数并使用if
和return
,或者使用第一种颜色然后使用第二种颜色
答案 1 :(得分:0)
它不是很漂亮,但你也可以交换你的陈述顺序,把前三个(更严格)放在第二个(不太严格)之下。那样,更严格的那些将覆盖不那么严格的那些,你应该得到预期的结果。
我仍然不喜欢它运行不必要的代码,但它是一个选项。 编辑:imho,建议的“其他如果”解决方案更好。
答案 2 :(得分:0)
正如其他人所评论的那样,你的条件含糊不清,可以通过在if块中应用更多逻辑来解决。
但是 - 每当我看到一堆if / then语句时,我退后一步,问我是否应该以不同的方式解决问题, 因为快速通过这种逻辑变得难以理解和不可维护。
对于不同的实现(只有你拥有的3种颜色),我会在头顶做一些这样的事情,这会减少经过的逻辑量并将颜色变化代码放在一个地方而不是通过代码散布它:
// declaration
[Flags]
enum ScreenColors
{
None = 0,
Red = 1,
Yellow = 2,
Blue = 4
}
private void button1_Click(object sender, EventArgs e)
{
ScreenColors selectedColors = ScreenColors.None;
if (red1.Checked || red2.Checked)
{
selectedColors |= ScreenColors.Red;
}
// etc for yellow, blue
// now I have a flags enum with only the 2 colours I care about
// so I can build a lookup table that says what combinations of source colors maps to what destination
Dictionary<ScreenColors, Color> lookups = new Dictionary<ScreenColors, Color>()
{
{ScreenColors.Red, Color.Red},
{ScreenColors.Yellow, Color.Yellow},
{ScreenColors.Blue, Color.Blue},
{ScreenColors.Red | ScreenColors.Blue, Color.Purple},
{ScreenColors.Red | ScreenColors.Yellow, Color.Orange},
{ScreenColors.Blue | ScreenColors.Yellow, Color.Green},
};
//... and now all I have to do is look up the flags in the dictionary to get the correct background
this.BackColor = lookups[selectedColors];
}