[C#] For Loop สามเหลี่ยมแบบต่างๆ









โค้ด

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

        private void buttonFront_Click(object sender, EventArgs e)
        {
            int cnt = 14;
            string strOut = "Loop = " + cnt.ToString() + " times\r\n";
            strOut += "- - - - - - - - - - - - - - \r\n";
            for (int i = 1; i <= cnt; i++)
            {
                strOut += i.ToString() + "\r\n";
            }
            textBox1.Text = strOut;
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            int cnt = 14;
            string strOut = "Loop = " + cnt.ToString() + " times\r\n";
            strOut += "- - - - - - - - - - - - - - \r\n";
            for (int i = cnt; i >= 1; i--)
            {
                strOut += i.ToString() + "\r\n";
            }
            textBox1.Text = strOut;
        }

        private void buttonStep_Click(object sender, EventArgs e)
        {
            int cnt = 25;
            string strOut = "Loop = " + cnt.ToString() + " times\r\n";
            strOut += "- - - - - - - - - - - - - - \r\n";
            for (int i = cnt; i >= 1; i-=3)
            {
                strOut += i.ToString() + "\r\n";
            }
            textBox1.Text = strOut;
        }

        private void buttonTable_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            for (int row = 1; row <= nudRow.Value; row++)
            {
                for (int col = 1; col <= nudCol.Value; col++)
                {
                    textBox1.Text += col.ToString() + " | ";
                }
                textBox1.Text += "\r\n";
            }
        }

        private void buttonTriangleUp_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            for (int j = 1; j <= (int)nudTriRow.Value; j++)
            {
                for (int i = 1; i <= j; i++)
                {
                    textBox1.Text += i.ToString() + "  ";
                }
                textBox1.Text += "\r\n";
            }

        }

        private void buttonTriangleDown_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            for (int j = (int)nudTriRow.Value; j >= 1; j--)
            {
                for (int i = 1; i <= j; i++)
                {
                    textBox1.Text += i.ToString() + "  ";
                }
                textBox1.Text += "\r\n";
            }
        }

        private void buttonTriangleRandom_Click(object sender, EventArgs e)
        {
            Random mdObj = new Random();
            int num = mdObj.Next(10);
            textBox1.Text = "";

            int row = 1;
            int col = 1;
            while (row <=num)
            {
                while (col <= row)
                {
                    textBox1.Text += num.ToString() + "    ";
                    col++;
                }
                textBox1.Text += "\r\n";
                row++;
                col = 1;
            }
            
            row = num;
            col = 1;
            while (row >= 1)
            {
                while (col <= row)
                {
                    textBox1.Text += num.ToString() + "    ";
                    col++;
                }
                textBox1.Text += "\r\n";
                row--;
                col = 1;
            }
        }
    }
}
Previous
Next Post »