| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 791 人关注过本帖
标题:求助C#计算器问题
只看楼主 加入收藏
飞不起的小鸟
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-11-2
结帖率:0
收藏
已结贴  问题点数:10 回复次数:8 
求助C#计算器问题
代码如下
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 简单模拟计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = "";
            label1.Text = "";

        }
        private void buttond_Click(object sender, EventArgs e)
        //单击数字命令按钮的事件处理程序
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;

        }



        private void button2_Click(object sender, EventArgs e)
        {

            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {

            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;
        }
        private void button5_Click(object sender, EventArgs e)
        {

            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;
        }



        private void button6_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;

        }

        private void button8_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;

        }

        private void button9_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;
        }

        private void button12_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;
        }

        private void button15_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;
        }

        private void button16_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + btn.Text;
        }
        private void buttonop_Click(object sender, EventArgs e)
        {
            
            Button btn = (Button)sender;
            
            if (btn.Name != "button12")//用户不单机“=”命令按钮
            {
                double x = Convert.ToDouble(textBox1.Text);
                string s = btn.Name;
                textBox1.Text = "";
                label1.Text = x.ToString();
            }
            else
            {
               
                if (label1.Text == "")
                    MessageBox.Show("输入不正确!!!", "信息提示", MessageBoxButtons.OK);
                else
                {
                    double y = Convert.ToDouble(textBox1.Text);
                    
                  
                    switch (s)//问题就在这个s,说上下文不存在名称s
                    {
                        case "button13"://用户刚前面单击“+”命令按钮
                            textBox1.Tex = (x + y).Tostring();
                            break;
                        case "button14"://用户刚前面单击“-”命令按钮
                            textBox1.Tex = (x - y).Tostring();
                            break;
                        case "button15"://用户刚前面单击“×”命令按钮
                            textBox1.Tex = (x * y).Tostring();
                            break;
                        case "button16"://用户刚前面单击“÷”命令按钮
                            if (y == 0)
                                MessageBox.Show("除零错误", "信息提示", MessageBoxButtons.OK);
                            else
                                textBox1.Tex = (x / y).Tostring();
                            break;
                    }
                    label1.Text = textBox1.Text;



                }
            }
  }
 }
  }
问题在那个变量s,我该怎么该才行
搜索更多相关主题的帖子: 计算器 private public 
2014-04-13 16:21
神奈创
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:67
专家分:191
注 册:2013-3-31
收藏
得分:4 
提示什么错误,试试string s = btn.Name.Tostring();....你有没有修改button13、14、15、16的变量名啊
2014-04-13 23:28
神奈创
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:67
专家分:191
注 册:2013-3-31
收藏
得分:0 
提示什么错误,试试string s = btn.Name.Tostring();....你有没有修改button13、14、15、16的变量名啊
2014-04-13 23:28
步向巅峰
Rank: 5Rank: 5
来 自:河南
等 级:贵宾
威 望:17
帖 子:110
专家分:371
注 册:2013-4-26
收藏
得分:4 
作用域问题
 if (btn.Name != "button12")//用户不单机“=”命令按钮
            {
                string s = btn.Name;//作用域只在当前所在模块,无法贯穿到模块2,将字符串的命名放到if语句外边就不会报错了
                    模块1;
            }
            else
            {
                    模块2;
            }
不过你都编到winform了,怎么还问这种问题,控制台的时候应该就接触到作用域这个问题了吧。另外你程序的可读性也太差了,过段时间估计你自己都看不懂自己写的程序了,把控件重命名最好,要不到后面控件多的话会感到很乱的
2014-04-14 17:35
飞不起的小鸟
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-11-2
收藏
得分:0 
回复 4楼 步向巅峰
就是因为把它放到if外也会报错我才问的!!
2014-04-14 21:30
步向巅峰
Rank: 5Rank: 5
来 自:河南
等 级:贵宾
威 望:17
帖 子:110
专家分:371
注 册:2013-4-26
收藏
得分:0 
我说的if是最外层的if,我试过了,没问题
2014-04-15 15:19
tzmyyu
Rank: 1
等 级:新手上路
帖 子:2
专家分:4
注 册:2012-10-21
收藏
得分:4 
这个论坛的字体怎么这么小,看着真费眼
2014-04-15 22:38
步向巅峰
Rank: 5Rank: 5
来 自:河南
等 级:贵宾
威 望:17
帖 子:110
专家分:371
注 册:2013-4-26
收藏
得分:0 
回复 7 楼 tzmyyu
win+"+"用放大镜看
2014-04-17 13:11
xiaowuge253
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-5-8
收藏
得分:0 
回复 4 楼 步向巅峰
版主威武!
2014-05-08 22:43
快速回复:求助C#计算器问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036035 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved