1. 局部編碼

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

        TextBox[,] tb1 = new TextBox[6, 6];
        TextBox[,] tb2 = new TextBox[3, 3];

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int y = 0; y < 6; y++)
            {
                {

                    var lbl = new Label();
                    lbl.Width = 10;
                    lbl.Text = y + "";
                    lbl.Left = 50 + 30 * y;
                    lbl.Top = 30;
                    groupBox1.Controls.Add(lbl);
                }

                {
                    var lbl = new Label();
                    lbl.Width = 10;
                    lbl.Text = y + "";
                    lbl.Top = 50 + 30 * y;
                    lbl.Left = 30;
                    groupBox1.Controls.Add(lbl);
                }

                for (int x = 0; x < 6; x++)
                {
                    var tb = new TextBox();
                    tb.Size = new Size(30, 30);
                    tb.Left = 50 + 30 * x;
                    tb.Top = 50 + 30 * y;
                    groupBox1.Controls.Add(tb);
                    tb1[x, y] = tb;
                }
            }

            for (int y = 0; y < 3; y++)
            {
                {

                    var lbl = new Label();
                    lbl.Width = 10;
                    lbl.Text = y + "";
                    lbl.Left = 50 + 30 * y;
                    lbl.Top = 30;
                    groupBox2.Controls.Add(lbl);
                }

                {
                    var lbl = new Label();
                    lbl.Width = 10;
                    lbl.Text = y + "";
                    lbl.Top = 50 + 30 * y;
                    lbl.Left = 30;
                    groupBox2.Controls.Add(lbl);
                }

                for (int x = 0; x < 3; x++)
                {
                    var tb = new TextBox();
                    tb.Size = new Size(30, 30);
                    tb.Left = 50 + 30 * x;
                    tb.Top = 50 + 30 * y;
                    groupBox2.Controls.Add(tb);
                    tb2[x, y] = tb;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int[,] d = new int[6, 6];
            for (int y = 0; y < 6; y++)
                for (int x = 0; x < 6; x++)
                {
                    d[x, y] = int.Parse(tb1[x, y].Text);
                }

            int[,] d2 = new int[3, 3];
            for (int i = 0; i < 9; i++)
                d2[i % 3, i / 3] = int.Parse(tb2[i % 3, i / 3].Text);

            int x1 = int.Parse(textBox1.Text);
            int y1 = int.Parse(textBox2.Text);

            int dc = d[x1 + 1, y1 + 1];

            int sum = 0;
            for (int i = 0; i < 9; i++)
            {
                int di = d[x1 + i % 3, y1 + i / 3];

                sum += T(di - dc) * d2[i % 3, i / 3];
            }

            textBox4.Text = sum.ToString();
        }

        int T(int i)
        {
            return i >= 0 ? 1 : 0;
        }
    }
}

Last updated