另外程序,我无法获得第一个点击的值 [英] In addition program,i cannot get the first clicked value

查看:62
本文介绍了另外程序,我无法获得第一个点击的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

救救我..........

我是c#.net的初学者.我正在尝试一个文本框的简单计算器程序,并在按钮中添加诸如add,sub之类的restig操作.我的问题是

Help me..........

Im a beginner to c#.net. Im trying simple calculator program with one textbox and remainig operations like add,sub are in buttons. My problem is

int total1=0;
        int total2=0;
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            total1 = total1 + int.Parse(txtAns.Text);
            total1 =Convert .ToInt32 (txtAns .Text .ToString ());
            txtAns.Text = string.Empty;
}
 protected void  btnEqual_Click(object sender, EventArgs e)
{
total1 = Convert.ToInt32(txtAns.Text);
            total2 = total1 + int.Parse(txtAns.Text);
            txtAns.Text = total2.ToString();
}



在此代码中调试时,单击添加按钮时,我将获得第一个值.但是当单击相等按钮时,我无法获得要添加的第一个值,只有第二个值得到了加倍....我不知道我的代码有什么问题.任何人都请引导我...​​.



While debug in this code, when add button is clicked, i''m getting first value. But when clicked on equal button, i cannot get that first value to add, only the second value getting double....i don''t know whats wrong in my code. Anybody please guide me....

推荐答案

很难说出您的代码是做什么的,它的作用是在每个步骤中都会造成混淆...
一些适用于您的规则:

1)如果某物是字符串,则在其上调用ToString没有意义-它不会做任何有用的事情. TextBox.Text已经是一个字符串-别管它了!
2)选择一种将字符串转换为int的方法,并坚持使用.使用两种执行相同操作的不同方法是毫无意义且令人困惑的.
3)不要编写使上一行与下一行撤消的代码-如果要在下一行覆盖它,那么在一行中为total1赋值是没有意义的.

因此,对您的代码进行了整理,并删除了多余的代码:
It''s difficult to tell what your code is meant to do, what it does is tries to confuse at every step...
Some rules for you:

1) If something is a string, there is no point in calling ToString on it - it won''t do anything useful. TextBox.Text is a string already - leave it alone!
2) Pick a method to convert from string to int, and stick with it. Using two different methods that do the same thing, is pointless and confusing.
3) Don''t write code that undoes the previous line with the next - there is no point is assigning a value to total1 in one line, if you are going to overwrite it in the next.

So, your code, tidied up a bit, and with redundant code removed:
int total1 = 0;
int total2 = 0;
protected void btnAdd_Click(object sender, EventArgs e)
    {
    total1 = int.Parse(txtAns.Text);
    txtAns.Text = string.Empty;
    }
protected void btnEqual_Click(object sender, EventArgs e)
    {
    total1 = int.Parse(txtAns.Text);
    total2 = total1 + int.Parse(txtAns.Text);
    txtAns.Text = total2.ToString();
    }


但是,我认为这并没有您真正想要做的.
我认为您想要做的是保持总计,并以某种方式每次添加.但是我不确定您到底想做什么. 解释一下?


However, I don''t think that this does what you actually want to to do.
I think what you want to do is keep a running total, and add to it each time, somehow. But I''m not sure exactly what you are trying to do. Care to explain?


我以为您的问题是txtAns.Text = string.Empty在添加按钮方法中.
I thought your problem is that txtAns.Text = string.Empty in the add-button method.


这篇关于另外程序,我无法获得第一个点击的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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