早上好, i有三个文本框用于firstnumber,secondnumber和result。 我有4个按钮加法,减法,乘法和除法。 如果我输入firstnumber和secondnumber并按下任何按钮,结果应显示在结果文本框上。 即如果我在第一个文本框中输入4,在第二个文本框中输入4并单击添加按钮,结果文本框中将显示结果8。 i我给我的代码
protected void Page_Load( object sender,EventArgs e) {} protected void btnadd_Click( object sender,EventArgs e) { txtresult.Text =(txtfirstnumber.Text)+(txtse condnumber.Text); } protected void btnsubtraction_Click( object sender,EventArgs e) { txtresult.Text =(txtfirstnumber.Text) - (txtsecondnumber.Text); } 受保护 void btnmultiplication_Click( object sender,EventArgs e) { txtresult.Text =(txtfirstnumber.Text)*(txtsecondnumber.Text); } 受保护 void btndivision_Click( object sender,EventArgs e) { txtresult.Text =(txtfirstnumber.Text)*(txtsecondnumber.Text); }请帮帮我。 谢谢。
解决方案TextBox的Text属性是一个字符串 - 所以将它们加在一起是行不通的(4+4= 44) 所以你需要将字符串转换为数字。 让我们假设你正在使用整数。 你可以做 int number1,number2; if ( int .TryParse(txtFirstNumnebr.Text, out number1)&&( int .TryParse(txtSecondNumber.Text, out number2)) { int result = number1 + number2; txtresult.Text = result.ToString( ); }
int.TryParse尝试将字符串转换为整数,如果失败则返回false,或者为true如果它工作,并将结果放在out参数(number1或number2) 我已经显示了一个''ADD''示例 - 同样需要也可以为所有其他操作完成 当然,你可以通过首先添加验证,错误信息等来大大提高 - 但在我的水平上猜测你是从你的问题,这应该让你开始。
txtfirstnumber.Text是System.String。 对于算术使用Convert.ToDouble(somestring)或double.TryParse(somestring,out value)。或者其他类型需要你。
good morning, i have three textboxes for firstnumber,secondnumber and result. I have 4 buttons addition,subtraction,multiplication and division. if i enter firstnumber and secondnumber and press on any button the result should be displayed on the result textbox. i.e. if i enter 4 in first textbox and 4 in second textbox and click on add button the result of 8 should be displayed on result textbox. i am giving my code
protected void Page_Load(object sender, EventArgs e) { } protected void btnadd_Click(object sender, EventArgs e) { txtresult.Text = (txtfirstnumber.Text) + (txtsecondnumber.Text); } protected void btnsubtraction_Click(object sender, EventArgs e) { txtresult.Text = (txtfirstnumber.Text) - (txtsecondnumber.Text); } protected void btnmultiplication_Click(object sender, EventArgs e) { txtresult.Text = (txtfirstnumber.Text) * (txtsecondnumber.Text); } protected void btndivision_Click(object sender, EventArgs e) { txtresult.Text = (txtfirstnumber.Text) * (txtsecondnumber.Text); }please help me. thank you.
解决方案 The Text property of a TextBox is a string - so adding them together doesn''t work ("4" + "4" = "44") So you need to convert the string to a number. Lets assume you are using integers. you can do int number1, number2; if (int.TryParse(txtFirstNumnebr.Text, out number1) && (int.TryParse(txtSecondNumber.Text, out number2)) { int result = number1 + number2; txtresult.Text = result.ToString(); }int.TryParse attempts to convert teh string to an integer, returning false if it fails, or true if it works, and puts the result in the out parameter (number1 or number2) I''ve shown an ''ADD'' example - the same would need to be done for all the other operations too Of course you could vastly improve on that by adding validation first, with error messages etc. - but at the level I am guessing you are at from your question, this should get you started.
txtfirstnumber.Text is System.String. For arithmatic use Convert.ToDouble(somestring) or double.TryParse(somestring, out value). Or eny other types need you.更多推荐
所有算术运算
发布评论