标题:求助C#计算器问题
取消只看楼主
飞不起的小鸟
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-11-2
结帖率:0
已结贴  问题点数:10 回复次数:1 
求助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: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-11-2
得分:0 
回复 4楼 步向巅峰
就是因为把它放到if外也会报错我才问的!!
2014-04-14 21:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-430044-1-1.html




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

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