2016-09-07 2 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

감사합니다. 나는 윈도우 폼에서 일하고 있는데 "Console.WriteLine ($"First place {item.Key} ");"을 사용할 필요가 없도록 레이블에 결과를 표시하고 싶습니다. " 그래서 나는 그 아이디어를 어떻게 할 지 모르겠다. @Dmitry Pokhlebaev – Becca

+0

Console 대신 FirstPlacetextBox, SecondPlacetextBox 등을 사용할 수 있습니다. 예를 들어 "FirstPlacetextBox.Text = string.Format ($"First place {item.key} ");" –

+0

참고로 두 줄의 코드가 변경되었습니다. var bestTime = results.Min (item => item.Value); var worstTime = results.Max (item => item.Value); –