2013-11-03 18 views
1

當涉及到c#時,我是一個完整的newb,並且在過去4個小時試圖創建一個方法而不是使用多個if/else語句的過程中掙扎。有人可以指出我的寫作方向嗎?如何使用我現有的代碼創建方法

基本上下面的代碼有5個輸入,當點擊button1時將計算它們的平方根。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace sqRoot 
{ 
    public partial class Form1 : Form 
    { 

     public Form1() 
     { 
      InitializeComponent(); 

     } 

     private void gatherTextBoxData() 
     { 
      double[] _lookup = new double[5]; 
      double doubleUserInput1; 
      double doubleUserInput2; 
      double doubleUserInput3; 
      double doubleUserInput4; 
      double doubleUserInput5; 


      if (Double.TryParse(textBox1.Text, out doubleUserInput1)) 
      { 
       double doubleUserSqRoot1 = Math.Sqrt(doubleUserInput1); 
       label2.Text = Convert.ToString(doubleUserSqRoot1); 

      } 
      else 
      { 
       label2.Text = textBox1.Text + " is not a number"; 
      } 

      if (Double.TryParse(textBox2.Text, out doubleUserInput2)) 
      { 
       double doubleUserSqRoot2 = Math.Sqrt(doubleUserInput2); 
       label3.Text = Convert.ToString(doubleUserSqRoot2); 

      } 
      else 
      { 
       label3.Text = textBox2.Text + " is not a number"; 
      } 

      if (Double.TryParse(textBox3.Text, out doubleUserInput3)) 
      { 
       double doubleUserSqRoot3 = Math.Sqrt(doubleUserInput3); 
       label4.Text = Convert.ToString(doubleUserSqRoot3); 

      } 
      else 
      { 
       label4.Text = textBox3.Text + " is not a number"; 
      } 

      if (Double.TryParse(textBox4.Text, out doubleUserInput4)) 
      { 
       double doubleUserSqRoot4 = Math.Sqrt(doubleUserInput4); 
       label5.Text = Convert.ToString(doubleUserSqRoot4); 

      } 
      else 
      { 
       label5.Text = textBox4.Text + " is not a number"; 
      } 

      if (Double.TryParse(textBox5.Text, out doubleUserInput5)) 
      { 
       double doubleUserSqRoot5 = Math.Sqrt(doubleUserInput5); 
       label6.Text = Convert.ToString(doubleUserSqRoot5); 

      } 
      else 
      { 
       label6.Text = textBox4.Text + " is not a number"; 
      } 


     } 


     private void button1_Click(object sender, EventArgs e) 
     { 
      gatherTextBoxData(); 

     } 


    } 
} 
+3

這個問題將是對codereview.stackexchange.com – pstrjds

回答

1

是這樣的,你正試圖實現?

方法:

private string processTextData(string value) 
{ 
    double temp; 
    if (double.TryParse(value, out temp)) 
    { 
     return Convert.ToString(Math.Sqrt(temp)); 
    } 
    else 
    { 
     return value + " is not a number"; 
    } 
} 

Usuage:

label1.Text = processTextData(textBox1.Text); 
+1

更好的擬合呃@馬克大廳我們認爲安靜的喜歡對方,我不會相信! – saeed

+0

謝謝馬克!我很感激。 –

+0

@this_guy不客氣,很高興有人幫忙 –

1

如果初始化文本框和標籤的數組,你可以在一個函數遍歷每個

// Assuming textBoxArr[], labelArr[], userValueArr are globals (not sure what class types) 

private void gatherTextBoxdata() 
{ 
    for(int i = 0; i < textBoxArr.Length; i++) 
    { 
     if (Double.TryParse(textBoxArr[i].Text)) 
     { 
      labelArr[i].Text = Convert.ToString(Math.Sqrt(doubleUserValueArr[i])); 

     } 
     else 
     { 
      labelArr[i].Text = textBoxArr[i].Text + " is not a number"; 
     } 
} 
1

嘗試這一點,可能會幫助你。

private string calculate(string text) 
    { 
     double _out; 
     if (double.TryParse(text, out _out)) 
     { 
      return Math.Sqrt(_out).ToString(); 
     } 
     return text + " is not a value"; 
    } 

,並點擊鏈接

private void button1_Click(object sender, EventArgs e) 
    { 
     label1.Text = calculate(textBox1.Text); 
     label2.Text = calculate(textBox2.Text); 
     label3.Text = calculate(textBox3.Text); 
     label4.Text = calculate(textBox4.Text); 
     label5.Text = calculate(textBox5.Text); 
    } 
相關問題