我正在试图找出计算一个简单的C#程序的第一,第二和第三位的最有效方法,其目的是找到获胜者,第二名和第三名并相应地显示他们的名字,对于这么简单的任务,我的代码似乎很大。我是新手,我使用If语句来完成所需的计算但是,我知道有更好的方法。有人可以开导我吗?
这是我当前的代码,在我意识到这将要采用的代码量之后我停下来了。
bash -c "mysql -h mysql-server -uuser -ppassword scans -N -B -e
'SELECT scan_id, count(*) FROM scan_info NATURAL JOIN found_results
WHERE fp_id = \"1669\" AND timestamp >= \"2016-08-31\"
ORDER BY scan_id LIMIT 1'"
}
答案 0 :(得分:0)
你有一个包含三个跑步者的列表,所以让.NET列表排序功能来帮你解决:
private class RunnersAndTimes
{
public string Name {get};
public double Time {get};
public RunnersAndTimes(string name, double time)
{
Time = time;
Name = name;
}
}
...
private void calculateButton_Click(object sender, EventArgs e)
{
var runnersAndTimes = new List<RunnersAndTimes> {
new RunnersAndTimes(runnerOneNameTextBox.Text,
double.Parse(runnerOneTimeTextBox.Text)),
new RunnersAndTimes(runnerTwoNameTextBox.Text,
double.Parse(runnerTwoTimeTextBox.Text)),
new RunnersAndTimes(runnerThreeNameTextBox.Text,
double.Parse(runnerThreeTimeTextBox.Text))
};
var orderedRunners = runnersAndTimes.OrderBy(runner => runner.Time).ToList();
firstPlaceLabel.Text = orderedRunners[0];
secondPlaceLabel.Text = orderedRunners[1];
thirdPlaceLabel.Text = orderedRunners[2];
}