如何使用现有代码创建方法

时间:2013-11-03 04:15:41

标签: c#

对于c#而言,我是一个完整的新手,并且在过去的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 个答案:

答案 0 :(得分: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 :(得分: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";
       }
}

答案 2 :(得分: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);
    }
相关问题