2014/11/28

11/28 第十週 使用陣列產生隨機變數

11/28  第十週  使用陣列產生隨機變數

一開始,先宣告一4*4的二維陣列以及隨機變數函數
再來進入到表單,設定每個按鈕的位置以及長寬
最後利用變數函數將設定值定在0~16
再把所有設定的內容ADD進去

EX:
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 WindowsFormsApplication10
{
    public partial class Form1 : Form
    {
        Button[,] Button = new Button[4, 4];
        Random irand = new Random();
        int rd = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int x;
            int width, height;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    width = this.Size.Width;
                    height = this.Size.Height;
                    Button[i, j] = new Button();
                    Button[i, j].Location = new Point(i * 50, j * 50);
                    x = (i + 1) * (j + 1);
                    width = 50;
                    height = 50;
                    Button[i, j].Size = new Size(width, height);
                    Button[i, j].Text = x.ToString();
                    rd = irand.Next(0, 16);
                    Button[i, j].Text = Convert.ToString(rd);
                    this.Controls.Add(Button[i, j]);
                }

            }
        }
    }
}




沒有留言:

張貼留言