四时宝库

程序员的知识宝库

源代码:C#编程获取二维数组的行列数,并生成随机数组

C#编程获取二维数组的行列数,并生成随机数组

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string[,] array1;

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            Random r = new Random();
            array1 = new string[r.Next(2,10), r.Next(2, 10)];
            label1.Text = string.Format("自动随机生成了{0}行{1}列的数组",array1.GetUpperBound(0)+1, array1.GetUpperBound(1) + 1);
            displayArray();
        }
        private void displayArray()
        {
            Random r = new Random();
            for(int i=0;i< array1.GetUpperBound(0) + 1;i++)
            {
                for (int j = 0; j < array1.GetUpperBound(1) + 1; j++)
                {
                    array1[i, j] = r.Next(1,10).ToString();
                    textBox1.Text = textBox1.Text+array1[i, j]+" ";
                }
                textBox1.Text = textBox1.Text + "\r\n";
                
            }
        }
    }
}

结语:

会使用二维数组,会随机生成区间数据,掌握获得数组的上界值的GetUpperBound方法。

喜欢的请关注和收藏!

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接