我有这样的下面的代码:如何优化此代码?
if (userValueSom01 == realResult01)
{
//answer = correct
//count +1 for overall good answers
WpfApplication1.Properties.Settings.Default.totallGood++;
//count for good +1
answerThisWindowGood++;
//make visible the green logo
Som01G.Visibility = Visibility.Visible;
}
else
{
//answer = wrong
//count +1 for overall wrong answers
WpfApplication1.Properties.Settings.Default.totallWrong++;
//count for wrong +1
answerThisWindowWrong++;
//make visible the red logo
Som01W.Visibility = Visibility.Visible;
labelSom01Check.Content = Convert.ToString(realResult01);
}
现在的观点是,这种情况发生XX次,其中XX是你的代码中显示的数字对应的数字。 所以在上面的例子中,XX是01. *注意,它的01在输入中,而01在结果中也是
不是很深入到c#(还),起初我认为当XX是20,我将需要复制上面这部分20次,并更改数字。 现在这似乎很繁琐,我想应该有一些更聪明的方式来处理这个问题,关键是,我不能想到如何(如上所述,我不是很深入到C#)。
任何能推动我走向正确方向的人?
谢谢你提前。
---编辑1 --- 谢谢Miika L. 从您的解决方案稍有不同:
public bool checkValue(double value, int result, Image controlG, Image controlW, Label label)
{
if (value == result)
{
//... Do stuff
controlG.Visibility = Visibility.Visible;
return true;
}
else
{
//... Do other stuff
controlW.Visibility = Visibility.Visible;
label.Content = result.ToString();
return false;
}
}
,现在我可以确实只要致电: 布尔测试=校验值(userValueSom01,realResult01, Som01G,Som01W,labelSom01Check);
作品:) thanx!
从上面的代码,没有太多的优化可以完成。你如何重复xx时间(记录在一个计数器中),然后设置值(由柜台) – 2012-01-16 08:33:32
ahaa,所以重复并使值++? – Dante1986 2012-01-16 08:34:53