| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
免费IT实战开发视频教程合集分享千里之行 始于足下
共有 650 人关注过本帖
标题:求解释,我的代码哪里出错了
只看楼主 加入收藏
半空的spider
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2013-4-28
结帖率:100%
  已结贴   问题点数:20  回复次数:4   
求解释,我的代码哪里出错了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Windows
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)     //数字1
        {
            textBox1.Text = textBox1.Text + button1.Text;
        }

        private void button2_Click(object sender, EventArgs e)     //数字2
        {
            textBox1.Text = textBox1.Text + button2.Text;
        }

        private void button3_Click(object sender, EventArgs e)     //数字3
        {
            textBox1.Text = textBox1.Text + button3.Text;
        }

        private void button4_Click(object sender, EventArgs e)    //数字4
        {
            textBox1.Text = textBox1.Text + button4.Text;
        }

        private void button5_Click(object sender, EventArgs e)    //数字5
        {
            textBox1.Text = textBox1.Text + button5.Text;
        }

        private void button6_Click(object sender, EventArgs e)    //数字6
        {
            textBox1.Text = textBox1.Text + button6.Text;
        }

        private void button7_Click(object sender, EventArgs e)     //数字7
        {
            textBox1.Text = textBox1.Text + button7.Text;
        }

        private void button8_Click(object sender, EventArgs e)     //数字8
        {
            textBox1.Text = textBox1.Text + button8.Text;
        }

        private void button9_Click(object sender, EventArgs e)     //数字9
        {
            textBox1.Text = textBox1.Text + button9.Text;
        }

        private void button10_Click(object sender, EventArgs e)    //数字0
        {
            textBox1.Text = textBox1.Text + button10.Text;
        }

        private void button11_Click(object sender, EventArgs e)   //加号
        {
            jisuanqi j = new jisuanqi();
            j.num1 = Convert.ToInt32(textBox1.Text);
            j.type = "+";
            textBox1.Text = "";

        }

        private void button12_Click(object sender, EventArgs e)   //减号
        {
            jisuanqi j = new jisuanqi();
            j.num1 = Convert.ToInt32(textBox1.Text);
            j.type = "-";
            textBox1.Text = "";
        }

        private void button13_Click(object sender, EventArgs e)    //乘号
        {
            jisuanqi j = new jisuanqi();
            j.num1 = Convert.ToInt32(textBox1.Text);
            j.type = "*";
            textBox1.Text = "";
        }

        private void button14_Click(object sender, EventArgs e)    //除号
        {
            jisuanqi j = new jisuanqi();
            j.num1 = Convert.ToInt32(textBox1.Text);
            j.type = "/";
            textBox1.Text = "";
        }

        private void button15_Click(object sender, EventArgs e)     //开平方
        {
            jisuanqi j = new jisuanqi();
            j.num1 = Convert.ToInt32(textBox1.Text);
            j.type = "开平方";
            textBox1.Text = "";
        }

        private void button16_Click(object sender, EventArgs e)     //等号
        {
            int n;
            jisuanqi j = new jisuanqi();
            j.num2 = Convert.ToInt32(textBox1.Text);
            if (j.type == button11.Text)
            {
                n = j.jiafa(j.num1,j.num2);
                textBox1.Text = n.ToString();
            }
           else if (j.type ==button12.Text)
            {
                if (j.num2 == 0)
                    MessageBox.Show("分母不能为零");
                else
                {
                    n = j.jianfa(j.num1, j.num2);
                    textBox1.Text = n.ToString();
                }
            }
            else if (j.type ==button13.Text)
            {
                n = j.chengfa(j.num1, j.num2);
                textBox1.Text = n.ToString();
            }
            else if (j.type ==button14.Text)
            {
                n = j.chufa(j.num1, j.num2);
                textBox1.Text = n.ToString();
            }
            else if (j.type == button15.Text)
            {
                if (j.num1 < 0)
                    MessageBox.Show("该数不能被开方");
                else
                {
                    double m = j.kaifang(j.num1);
                    textBox1.Text = m.ToString();
                }
            }
        }
    }
}
public class jisuanqi
{
    public int num1;
    public int num2;
    public string type;
    public int jiafa()
    {
        return num1 + num2;
    }
    public int jiafa(int a, int b)
    {
        this.num1 = a;
        this.num2 = b;
        return (num1 + num2);
    }
    public int jianfa()
    {
        return num1 - num2;
    }
    public int jianfa(int a, int b)
    {
        this.num1 = a;
        this.num2 = b;
        return (num1 - num2);
    }
    public int chengfa()
    {
        return num1 * num2;
    }
    public int chengfa(int a, int b)
    {
        this.num1 = a;
        this.num2 = b;
        return (num1 * num2);
    }
    public int chufa()
    {
        return num1 / num2;
    }
    public int chufa(int a, int b)
    {
         this.num1 = a;
        this.num2 = b;
        return (num1 / num2);
    }
    public double kaifang(int a)
    {
        this.num1 = a;
        return Math.Sqrt(num1);
    }
}
为什么我按等号它没有结果出来,求解释。。。。
搜索更多相关主题的帖子: void  namespace  Windows  private  public  
2013-06-01 11:02
moridiansha
Rank: 6Rank: 6
来 自:承德
等 级:侠之大者
威 望:4
帖 子:242
专家分:417
注 册:2009-10-21
  得分:4 
int n;
            jisuanqi j = new jisuanqi();
            j.num2 = Convert.ToInt32(textBox1.Text);
            if (j.type == button11.Text) //这里貌似j.type是空值
            {
}

www.qysy.tk
2013-06-01 11:16
shangsharon
Rank: 9Rank: 9Rank: 9
来 自:湖北武汉
等 级:蜘蛛侠
威 望:7
帖 子:221
专家分:1261
注 册:2012-3-25
  得分:6 
private void button1_Click(object sender, EventArgs e)     //数字1
        {
            textBox1.Text = textBox1.Text + button1.Text;
        }
10个数字就写了10个方法,而且方法几乎一模一样.

 private void button_Click(object sender, EventArgs e)     //数字0-9
        {
            textBox1.Text = textBox1.Text + (sender as Button).Text;
        }
将0-9每个按钮的点击事件关联到这个方法是不是简单多了,代码看着舒服些吧,省了几行代码也不错哦.
2013-06-01 12:17
黄辉
Rank: 4
等 级:业余侠客
帖 子:106
专家分:255
注 册:2013-4-28
  得分:4 
在加减乘除开方那里应该加一个标识符,这样在if语句中就可以判断了

你不勇敢,谁替你坚强。。。
2013-06-01 12:39
QJlin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:186
专家分:560
注 册:2013-5-18
  得分:6 
int n;
            jisuanqi j = new jisuanqi();
            j.num2 = Convert.ToInt32(textBox1.Text);
你这个有实例化了jisuanqi里面的type当然是空的,要你就把它声明为全局变量!

慢慢前进走,不求一步登天,只求慢慢前进
2013-06-01 12:55







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

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