1

I am a complete newb when it comes to c# and have been struggling for the past 4 hours trying to create a method instead of using multiple if/else statements. Can anybody point me in the write direction?

Basically the following code has 5 inputs that will calculate their square root when button1 is clicked.

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();

        }


    }
}
4

3 回答 3

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

用法:

label1.Text = processTextData(textBox1.Text);
于 2013-11-03T04:28:03.223 回答
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";
       }
}
于 2013-11-03T04:28:29.123 回答
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);
    }
于 2013-11-03T04:30:32.373 回答