使用阵列计算器 [英] Calculator using an array

查看:125
本文介绍了使用阵列计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,一个文本框和一个标签。单击该按钮时,应计算为在文本框中并输出标签上​​的价值。我的主要问题是如何分割字符串,这样,当我键入12 + 12将其分割成1212然后计算出答案。

我迄今所做的办法是所有的文字分成字符。

 的char []×;
的for(int i = 0; I< textBox1.TextLength;我++){
    X = textBox1.Text.ToCharArray();
    如果(X [I]的ToString()!=+)
    {
    }其他{    }
}


解决方案

你可以这样对加,并请首先检查:

 字符串str = yourTextBoxValue.Text;
字符串[] =可疑交易报告str.Split(新的char [] {'+'},StringSplitOptions.RemoveEmptyEntries);
INT参数1 = int.Parse序列(STR [0]);
INT参数2 = int.Parse序列(STR [1]);
INT结果=参数1 + param2的;
yourLabel.Text = result.ToString();

I have a button a textbox and a label. when the button is clicked, it should compute for the value in the textbox and output it on the label. My main problem is how to split the string so that when I typed "12+12" it would split it into "12" and "12" then compute the answer.

The approach I've done so far is to split all the text into characters.

char[] x;


for (int i = 0; i < textBox1.TextLength; i++) {
    x = textBox1.Text.ToCharArray();
    if (x[i].ToString() != "+")
    { 
    } else { 

    }
}

解决方案

you could do this to plus, and please do check first:

string str = yourTextBoxValue.Text;
string[] strs = str.Split(new char[] { '+' }, StringSplitOptions.RemoveEmptyEntries);
int param1 = int.Parse(strs[0]);
int param2 = int.Parse(strs[1]);
int result = param1 + param2;
yourLabel.Text = result.ToString();

这篇关于使用阵列计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆