1
我想找出最有效的方法來計算一個簡單的C#程序的第一,第二和第三個地方,其目的是找到勝利者,第二名,和第三名,然後顯示他們的名字,然而,我的代碼似乎很容易做到這麼簡單的任務。我是新的,我使用If語句來完成所需的計算,但是,我知道有一個更好的方法。有人能夠啓發我嗎?比較並計算第一,第二和第三位
這是我目前的代碼,並且在實現了代碼量之後我停止了。
private void calculateButton_Click(object sender, EventArgs e)
{
// Define Name and Time Variables
string runnerone = runnerOneNameTextBox.Text; // Runner One Name
string runnertwo = runnerTwoNameTextBox.Text; // Runner Two Name
string runnerthree = runnerThreeNameTextBox.Text; // Runner Three Name
double runnerOneTime = double.Parse(runnerOneTimeTextBox.Text); // Runner One Time
double runnerTwoTime = double.Parse(runnerTwoTimeTextBox.Text); // Runner Two Time
double runnerThreeTime = double.Parse(runnerThreeTimeTextBox.Text); // Runner Three Time
//-------------------------------------------------------------------------
// Start of the If statement to calculate who is first, second, and third.
//-------------------------------------------------------------------------
// FIRST PLACE CODE:
if (runnerOneTime > runnerTwoTime && runnerOneTime > runnerThreeTime) // Runner One is greater than everyone
{
firstPlaceLabel.Text = runnerOneNameTextBox.Text;
firstPlaceTrophyLabel.Text = runnerOneNameTextBox.Text;
}
else if (runnerOneTime == runnerTwoTime && runnerOneTime > runnerThreeTime) // Runner one is equal to runner two
{
firstPlaceLabel.Text = runnerOneNameTextBox.Text;
firstPlaceLabel.Text = runnerTwoNameTextBox.Text;
firstPlaceTrophyLabel.Text = runnerOneNameTextBox.Text;
firstPlaceTrophyLabel.Text = runnerTwoNameTextBox.Text;
}
else if (runnerOneTime > runnerTwoTime && runnerOneTime == runnerThreeTime)
}
}
}
謝謝。這是我現在所知道的一點點,但是,我會研究這個廢話並使其工作。非常感謝你! –