2016-09-07 17 views
0

テキストボックスからランナータイムを取得し、結果のテキストボックスに降順で結果を配置しようとしています。注文は1位、2位、3位にする必要があります。私は条件文を使ってみましたが、たくさんのコードを使わなければならないようです。 1位、2位、3位を決める方法を見つける別の方法はありますか?ここに私のコードです。入力テキストボックスとプレースバリュー結果のテキストボックスの最高値と最低値を取得

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 = ""; 

答えて

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; 
    } 

} 
+0

感謝。私はWindowsフォームで作業しているので、私は "Console.WriteLine($" First place {item.Key} ");)を使用する必要はないので、ラベルに結果を表示したい"だから私はどのようにそのアイデアを行うか分からないのですか? @Dmitry Pokhlebaev – Becca

+0

Consoleの代わりにFirstPlacetextBox、SecondPlacetextBoxなどを使用できます。たとえば、 "FirstPlacetextBox.Text = string.Format($" First place {item.key} ");" –

+0

私は自分のコードの2行を変更したことに注意してください: var bestTime = results.Min(item => item.Value); var worstTime = results.Max(item => item.Value); –

関連する問題