获取输入文本框中的最高和最低值以及放置值结果文本框

时间:2016-09-07 02:13:01

标签: c# .net

我正在尝试从文本框中获取Runner Time,并将结果按降序排列在结果文本框中。订单应该是第一名,第二名和第三名。我尝试使用条件语句,但似乎我将不得不使用很多代码。还有另一种方法可以弄清楚如何确定第一名,第二名和第三名......这是我的代码。

namespace Calculate Runner Time
{
    public partial class Form1 : Form
    {
        public Form1()
        {

            InitializeComponent();

        }

        private void Calculatebutton_Click(object sender, EventArgs e)
        {

            string Runner1Name;
            string Runner2Name;
            string Runner3Name;

            double Runner1Time;
            double Runner2Time;
            double Runner3Time;

            //double FirstPlace;
            //double SecondPlace;
            //double ThirdPlace;

          //get runner names
            Runner1Name = Runner1NametextBox.Text;
            Runner2Name = Runner2NametextBox.Text;
            Runner3Name = Runner3NametextBox.Text;



            //check if Runner1Name is empty 
            if (string.IsNullOrEmpty(Runner1Name))
            {
                 MessageBox.Show("The Runner 1 Name cannot be empty ", "Invalid Runner Name",
                 MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            //check if Runner1Name is empty 
            else if (string.IsNullOrEmpty(Runner2Name)) 
            {
                MessageBox.Show("The Runner 2 Name cannot be empty ", "Invalid Runner Name",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else if (string.IsNullOrEmpty(Runner3Name))
            {
               MessageBox.Show("The Runner 3 Name cannot be empty ", "Invalid Runner Name",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else if (!double.TryParse(Runner1TimetextBox.Text, out Runner1Time)) 

            {
                MessageBox.Show("Please Input a Positive number for Runner 1", "Invalid Input",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else if  (!double.TryParse(Runner2TimetextBox.Text, out Runner2Time))
            {
                MessageBox.Show("Please Input a Positive number for Runner 2", "Invalid Input",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else if  (!double.TryParse(Runner3TimetextBox.Text, out Runner3Time))
            {
                MessageBox.Show("Please Input a Positive number for Runner 3", "Invalid Input",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            //else if (Runner1Time == Runner2Time & Runner1Time == Runner3Time)
            //{
            //    FirstPlacetextBox.Text = Runner1Name.ToString();
            //    FirstPlacetextBox.Text = Runner2Name.ToString();
            //    FirstPlacetextBox.Text = Runner3Name.ToString();
            //}

            else if (Runner1Time >= Runner2Time & Runner1Time >= Runner3Time)              
            {
                FirstPlacetextBox.Text = Runner1Name.ToString();                         
            }

            else if (Runner2Time >= Runner1Time & Runner2Time >= Runner3Time)
            {
                FirstPlacetextBox.Text = Runner2Name.ToString();
            }

            else if  (Runner3Time >= Runner2Time & Runner3Time >= Runner1Time)
            {
                FirstPlacetextBox.Text = Runner3Name.ToString();
            }

            else if (Runner1Time <= Runner2Time & Runner1Time <= Runner3Time)
            {           

                SecondPlacetextBox.Text = Runner1Name.ToString();
            }

            else if (Runner2Time <= Runner1Time & Runner2Time <= Runner3Time)
            {
                SecondPlacetextBox.Text = Runner2Name.ToString();
            }

            else if (Runner3Time <= Runner2Time & Runner3Time <= Runner1Time)
            {
                SecondPlacetextBox.Text = Runner3Name.ToString();
            }

          //  else if ()
            {

            }
        }

        private void Closebutton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Resetbutton_Click(object sender, EventArgs e)
        {
            //clear runnername textboxes 
            Runner1NametextBox.Text = string.Empty;
            Runner2NametextBox.Text = string.Empty;
            Runner3NametextBox.Text = string.Empty;


            //clear runnertime 
            Runner1TimetextBox.Text = "";
            Runner2TimetextBox.Text = "";
            Runner3TimetextBox.Text = "";


            //clears result textbox 
            FirstPlacetextBox.Text = "";
            SecondPlacetextBox.Text = "";
            ThirdPlacetextBox.Text = "";

1 个答案:

答案 0 :(得分:0)

你可以这样做

Dictionary<string, double> results = new Dictionary<string, double>();

// Add your runners.
results.Add(Runner1Name, Runner1Time);
results.Add(Runner2Name , Runner2Time);
results.Add(Runner3Name , Runner2Time);

var bestTime = results.Min(item => item.Value);
var worstTime = results.Max(item => item.Value);
foreach (var item in results)
{
    if (item.Value == bestTime)
    {
        Console.WriteLine($"First place {item.Key}");
        continue;
    }
    if (item.Value == worstTime)
    {
        Console.WriteLine($"Third place {item.Key}");
        continue;
    }
    Console.WriteLine($"Second place {item.Key}");
}

您的功能代码:

private void Calculatebutton_Click(object sender, EventArgs e)
{
    string Runner1Name;
    string Runner2Name;
    string Runner3Name;

    double Runner1Time;
    double Runner2Time;
    double Runner3Time;

    //double FirstPlace;
    //double SecondPlace;
    //double ThirdPlace;

  //get runner names
    Runner1Name = Runner1NametextBox.Text;
    Runner2Name = Runner2NametextBox.Text;
    Runner3Name = Runner3NametextBox.Text;



    //check if Runner1Name is empty 
    if (string.IsNullOrEmpty(Runner1Name))
    {
         MessageBox.Show("The Runner 1 Name cannot be empty ", "Invalid Runner Name",
         MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    //check if Runner1Name is empty 
    else if (string.IsNullOrEmpty(Runner2Name)) 
    {
        MessageBox.Show("The Runner 2 Name cannot be empty ", "Invalid Runner Name",
        MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    else if (string.IsNullOrEmpty(Runner3Name))
    {
       MessageBox.Show("The Runner 3 Name cannot be empty ", "Invalid Runner Name",
       MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    else if (!double.TryParse(Runner1TimetextBox.Text, out Runner1Time)) 

    {
        MessageBox.Show("Please Input a Positive number for Runner 1", "Invalid Input",
        MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    else if  (!double.TryParse(Runner2TimetextBox.Text, out Runner2Time))
    {
        MessageBox.Show("Please Input a Positive number for Runner 2", "Invalid Input",
        MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    else if  (!double.TryParse(Runner3TimetextBox.Text, out Runner3Time))
    {
        MessageBox.Show("Please Input a Positive number for Runner 3", "Invalid Input",
        MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    Dictionary<string, double> results = new Dictionary<string, double>();

    results.Add(Runner1Name, Runner1Time);
    results.Add(Runner2Name , Runner2Time);
    results.Add(Runner3Name , Runner2Time);

    var bestTime = results.Min(item => item.Value);
    var worstTime = results.Max(item => item.Value);
    foreach (var item in results)
    {
        if (item.Value == bestTime)
        {
            FirstPlacetextBox.Text = item.Key;
            continue;
        }
        if (item.Value == worstTime)
        {
            ThirdPlacetextBox.Text = item.Key;
            continue;
        }
        SecondPlacetextBox.Text = item.Key;
    }

}