索引超出了数组界限。。
 再做一个计算机的小程序 ,初学者请多指点;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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string s;
        int i = -1;                public Form1()
        {
            InitializeComponent();
        }        private void button0_Click(object sender, EventArgs e)
        {
            i++;
            textBox1.Text += button0.Text;
            array[j] = button0.Text;
            j++;
            s = textBox1.Text; 
        }        private void button1_Click(object sender, EventArgs e)
        {
            i++;
            textBox1.Text += button1.Text;
            array[j] = button0.Text;
            j++;
            s = textBox1.Text;  
        }        private void button2_Click(object sender, EventArgs e)
        {
            i++;
            textBox1.Text += button2.Text; 
            array[j] = button0.Text;
            j++;
            s = textBox1.Text;
        }        private void button3_Click(object sender, EventArgs e)
        {
            i++;
            textBox1.Text += button3.Text;
            array[j] = button0.Text;
            j++;
            s = textBox1.Text;
        }        private void button4_Click(object sender, EventArgs e)
        {
            i++;
            textBox1.Text += button4.Text;
            array[j] = button0.Text;
            j++;
            s = textBox1.Text;
        }        private void button5_Click(object sender, EventArgs e)
        {
            i++;
            textBox1.Text += button5.Text;
            array[j] = button0.Text;
            j++;
            s = textBox1.Text;
        }        private void button6_Click(object sender, EventArgs e)
        {
            i++;
            textBox1.Text += button6.Text;
            array[j] = button0.Text;
            j++;
            s = textBox1.Text;
        }        private void button7_Click(object sender, EventArgs e)
        {
            i++;
            textBox1.Text += button7.Text;
            array[j] = button0.Text;
            j++;
            s = textBox1.Text;
        }        private void button8_Click(object sender, EventArgs e)
        {
            i++;
            textBox1.Text += button8.Text;
            array[j] = button0.Text;
            j++;
            s = textBox1.Text;
        }        private void button9_Click(object sender, EventArgs e)
        {
            i++;
            textBox1.Text += button9.Text;
            array[j] = button0.Text;
            j++;
            s = textBox1.Text;
        }        private void button10_Click(object sender, EventArgs e)
        {
            i++;
            textBox1.Text += button10.Text;
            array[j] = button0.Text;
            j++;
            s = textBox1.Text;
        }        private void button11_Click(object sender, EventArgs e)
        {
            i++;
            textBox1.Text += button11.Text;
            array[j] = button0.Text;
            j++;
            s = textBox1.Text;
        }        private void button12_Click(object sender, EventArgs e)
        {
            i++;
            textBox1.Text += button12.Text; 
            array[j] = button0.Text;
            j++;
            s = textBox1.Text;
        }        private void button13_Click(object sender, EventArgs e)
        {
            i++;
            textBox1.Text += button13.Text;
            array[j] = button0.Text;
            j++;
            s = textBox1.Text;
        }        private void button14_Click(object sender, EventArgs e)
        {
            i++;
            textBox1.Text += button14.Text;
            array[j] = button0.Text;
            j++;
            s = textBox1.Text;
        }        private void button15_Click(object sender, EventArgs e)
        {
            jieguo1();
        }        private void textBox1_TextChanged(object sender, EventArgs e)
        {        }        decimal a, b;
        int j = 0;
        string[] array = new string[10];
        int k = 0;        string m = "";
        string n = "";        private void jieguo1()
        {
            
            for (; ; k++)
            {
                switch (array[k])
                {
                    case "0":
                        m += "0";
                        break;
                    case "1":
                        m += "1";
                        break;
                    case "2":
                        m += "2";
                        break;
                    case "3":
                        m += "3";
                        break;
                    case "4":
                        m += "4";
                        break;
                    case "5":
                        m += "5";
                        break;
                    case "6":
                        m += "6";
                        break;
                    case "7":
                        m += "7";
                        break;
                    case "8":
                        m += "8";
                        break;
                    case "9":
                        m += "9";
                        break;
                    case "+":
                        jieguo2(1);
                        break;
                    case "-":
                        jieguo2(2);
                        break;
                    case "*":
                        jieguo2(3);
                        break;
                    case "/":
                        jieguo2(4);
                        break;                }
            }
        }        private void jieguo2(int c)
        {
            
            for (; ; k++)
            {
                switch (array[k])
                {
                    case "0":
                        n += "0";
                        break;
                    case "1":
                        n += "1";
                        break;
                    case "2":
                        n += "2";
                        break;
                    case "3":
                        n += "3";
                        break;
                    case "4":
                        n += "4";
                        break;
                    case "5":
                        n += "5";
                        break;
                    case "6":
                        n += "6";
                        break;
                    case "7":
                        n += "7";
                        break;
                    case "8":
                        n += "8";
                        break;
                    case "9":
                        n += "9";
                        break;
                    case "=":
                        a=Convert .ToDecimal (m);
                        b=Convert .ToDecimal (n);
                        jieguo2(a, b, c);
                        break;                }
            }        }        private void jieguo2(decimal  x, decimal  y, decimal  z)
        {
            decimal q;
            if (z == 1)
            {
                q = a - b;
                textBox1.Text = q.ToString();
            }            if (z == 2)
            {
                q = a + b;
                textBox1.Text = q.ToString();
            }            if (z == 3)
            {
                q = a * b;
                textBox1.Text = q.ToString();
            }            if (z == 4)
            {
                q = a / b;
                textBox1.Text = q.ToString();
            }
 
        }
        
    }
}

解决方案 »

  1.   

    你这个代码有点纠结了,我给你改了下
            private void button0_Click(object sender, EventArgs e)
            {
                textBox1.Text += button0.Text;
            }        //button1到button14的代码完全相同,这里省略了。。          private void button15_Click(object sender, EventArgs e)
            {
                string str = textBox1.Text.Trim();
                while (str.EndsWith("="))
                    str=str.Remove(str.Length - 1, 1);
                System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"^(\d+)\+(\d+)$");
                System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@"^(\d+)\1(\d+)$");
                System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@"^(\d+)\*(\d+)$");
                System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"^(\d+)\/(\d+)$");            System.Text.RegularExpressions.Match m;
                if ((m = regex1.Match(str)).Success)
                {
                    textBox1.Text = (int.Parse(m.Groups[1].Value) + int.Parse(m.Groups[2].Value)).ToString();
                }
                else if ((m = regex2.Match(str)).Success)
                {
                    textBox1.Text = (int.Parse(m.Groups[1].Value) - int.Parse(m.Groups[2].Value)).ToString();
                }
                else if ((m = regex3.Match(str)).Success)
                {
                    textBox1.Text = (int.Parse(m.Groups[1].Value) * int.Parse(m.Groups[2].Value)).ToString();
                }
                else if ((m = regex4.Match(str)).Success)
                {
                    textBox1.Text = (int.Parse(m.Groups[1].Value) / int.Parse(m.Groups[2].Value)).ToString();
                }
                else
                {
                    MessageBox.Show("格式错误,正确为:a+b;a-b;a*b;a/b");
                    textBox1.Text = "";
               }
            } 
      

  2.   

    System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@"^(\d+)\1(\d+)$");
    改成----》》》
    System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@"^(\d+)\-(\d+)$");