点击一个按钮,要把一个textBox中的整数和一个2维数组中的数字作比较,算出这个数在这个2维数组中出现的概率,再把概率显示在另外一个textBox中应该怎么写呢  
对二维数组不熟悉 谢谢 

解决方案 »

  1.   

    int[,] a = new int[M,N];
    ....赋值
    int count = 0;
    for(int i=0;i<M;i++)
    {
        for(int j=0;j<N;j++)
        {
            if(a[i,j]==你输入的数值) count++;
        }
    }double 概率 = (double)count / (M*N);
    textbox2.Text = 概率.ToString();
      

  2.   

    用两个for或者Foreach就可以了。
      

  3.   

    int[,] a = new int[M,N]; 这个就是定义二维数组吗
      

  4.   

            int[][] test = { new int[] { 1, 2, 3, 4, 5, 6, 2, 4, 5 }, new int[] { 44, 5, 24, 4, 2, 4, 6, 7, 2, 4, 5 }, new int[] { 3, 4, 1, 2, 4, 6, 2, 4 } };
            int num = 4;
            int i = 0, total = 0;
            foreach (int[] arr in test)
            {
                i += (from t in arr
                      where t == num
                      select t).Count();
                total += arr.Length;
            }
            Console.WriteLine((float)i / total);
      

  5.   

    int[][] test = { new int[] { 1, 2, 3, 4, 5, 6, 2, 4, 5 }, new int[] { 44, 5, 24, 4, 2, 4, 6, 7, 2, 4, 5 }, new int[] { 3, 4, 1, 2, 4, 6, 2, 4 } };
    int num = 4;
    int count = 0;
    foreach(int n in arr)
    {
        if(n==num) count++;
    }
    count就是结果。对二维数组,可以用迭代器直接逐个访问的。
      

  6.   


    2楼的是正解!
    二维数组是按照你定义的类型的一组数,比如 int[M,N]那就是说一个M行N列,每一个元素都是一个整型数的数组!
    int[K][]是交叉数组,意思是这个数组有K个元素,每一个元素都是一个整型的数组,但是长度可以不一样.
      

  7.   

    string[,] array = new string[4, 4];var result= from str in array.Cast<string>()
      where int.Parse(str)==1
      select str;
     或
    int [,] array = new int [2,2];  
    var p= from i in array.Cast< int >() where i == 2 select i;   
    查询,在计算概率 
     for (int i = 0; i < ary.GetLength(0); i++)
      {
      for (int j = 0; j < ary.GetLength(1); j++)
      {
      if(ary[i, j]==值){count++;}
      }
      }