首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# 索引超出了数组界限,该如何解决

2014-01-08 
c# 索引超出了数组界限初学编程,不怎么会。求助,修改。要做一个公式的计算的窗口,计算概率的,求概率的最大值

c# 索引超出了数组界限
初学编程,不怎么会。求助,修改。
要做一个公式的计算的窗口,计算概率的,求概率的最大值。
输入:a[6],6个元素之不是1就是0。a[1]对应p[1],p[1]是概率,a[2]对应p[2],a[3]对应p[3],a[4]对应p[4]……,组成概率数组p[6].求其中概率最大的。

公式: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.Windows.Forms;

namespace bayes01
{

    public partial class Form1 : Form
    {
        
        int[] nArray = new int[6];//定义一个int型数组  
        public Form1()
        {

            InitializeComponent();
            String str = textBox1.Text;
             char[] ch = str.ToCharArray();//转换成char型来计算  
             //int[] nArray = new int[str.Length];//定义一个int型数组  

             const int ten = 10;   //定义常量  
             for (int i = 0; i < ch.Length; i++)
             {

                 nArray[i] = ch[i] - 48;  //把字符转换成int  
                 for (int j = ch.Length - i - 1; j > 0; j--)
                 {
                     nArray[i] *= ten;
                 }

             }  
            
       }    
        
        
        static double cimi(double p,int m)//连续相乘
        {
            double sum = 1;
            for(int i=0;i<m;i++)
            {
                
                sum *= p;
            }
            return sum;
        }
         private int mishu1(int[] m)//前一项的次幂数
        {
            int i,j;
            for (i = 1; i <= 6; i++)
            {
                for (j = 1; j <= i; j++)
                {
                    if ((nArray[i]) == 0)
                        m[i] += 1;
                }
                for (j = i + 1; j <= 6; j++)
                {
                    if (nArray[i] == 1)
                        m[i] += 1;
                }
            }
            return m[i];


           
        }
        private int mishu2(int[] n)//后一项的次幂数
        {
            int i,j;
            for (i = 1; i <= 6; i++)
            {
                for (j = 1; j <= i; j++)
                {
                    if (nArray[i] == 1)
                        n[i] += 1;
                }
                for (j = i + 1; j <= 6; j++)
                {
                    if (nArray[i] == 0)
                        n[i] += 1;
                }
            }
            return n[i];
        }
        private double  zuida(double[] array,int m)
        {
            double  pmax=0;
           
        
            double[] p=new double[6];
            for (int i = 1; i <=6; i++)
            {
                if (p[i] > pmax)
                {
                    pmax = p[i];
                    m = i;
                }
            }
            return pmax;
        }

        private void button1_Click(object sender, EventArgs e)
         {
             double Pmax;
             int res=0;//概率最大组的序号
             double q = 0.000001;//q
             double[] p = new double[6];//6个开关发生故障概率
             for (int i=0;i<6;i++)
             {  int[] m=new int[6];
             int[] n = new int[6];
             p[i]=((cimi(q,mishu1(m)))*(cimi((1-q),mishu2(n))));
             }
              Pmax=zuida(p,res);
              textBox2.Text = Pmax.ToString();
         }

       
        
    }
}



自己编的烂程序实在是看不下去了,大家帮帮忙吧。学编程,感觉好困难。唉。c# 索引超出了数组界限,该如何解决
本来我就没多少分,希望大神不吝赐教。谢谢


[解决办法]
qq: 546445693

[解决办法]
 public partial class Form1 : Form
    {
        int[] nArray = new int[6];//定义一个int型数组  

        int[] m = new int[6];
        int[] n = new int[6];
      
        public Form1()
        {

            InitializeComponent();
            String str = textBox1.Text;
            char[] ch = str.ToCharArray();//转换成char型来计算  
            //int[] nArray = new int[str.Length];//定义一个int型数组  

            const int ten = 10;   //定义常量  
            for (int i = 0; i < ch.Length; i++)
            {

                nArray[i] = ch[i] - 48;  //把字符转换成int  
                for (int j = ch.Length - i - 1; j > 0; j--)
                {
                    nArray[i] *= ten;
                }

            }

        }

        static double cimi(double p, int m)//连续相乘
        {
            double sum = 1;
            for (int i = 0; i < m; i++)
            {
                sum *= p;
            }
            return sum;
        }
        private int mishu1(int[] m)//次幂数---你那个写重复了而且逻辑不对
        {
            int i, j;

            for (i = 0; i <6; i++)
            {
                    if (nArray[i] == 1)
                        m[i] += 1;
            }
            return m[i];

        }
        
        private double zuida(double[] array, int m)
        {
            double pmax = 0;


            double[] p = new double[6];
            for (int i = 1; i <= 6; i++)
            {
                if (p[i] > pmax)
                {
                    pmax = p[i];
                    m = i;


                }
            }
            return pmax;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            double Pmax;
            int res = 0;//概率最大组的序号
            double q = 0.000001;//q
            double[] p = new double[6];//6个开关发生故障概率
           
            for (int i = 0; i < 6; i++)////你直接调用瀚数得到幂数
            {
                p[i] = ((cimi(q, mishu1(m))) * (cimi((1 - q), mishu2(n))));
            }

           

            Pmax = zuida(p, res);
            textBox2.Text = Pmax.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

这样看看
[解决办法]
楼主,我写完了,代码里面有注解(NOTE)
注意,我这里是控制台应用程序,Execute 方法即是你的 Button_Click 方法,你应该稍作改动


private static readonly int ArrayLength = 6;
private static int[] nArray = new int[ArrayLength];
private static readonly int DecimalBase = 10;
private static void Main(string[] args)
{
    Console.Write("Please input a[{0}]: ", ArrayLength);
    // Get a content
    char[] chars;
    while ((chars = Console.ReadLine().ToCharArray()).Length != ArrayLength)
    {
        Console.WriteLine("Invalid values, value should be '0' or '1'.");
        Console.Write("Please input a[{0}]: ", ArrayLength);
    }
    for (int i = 0; i < chars.Length; i++)
    {
        nArray[i] = chars[i] - 48;
        for (int j = chars.Length - i - 1; j > 0; j--)
        {
            nArray[i] *= DecimalBase;
        }
    }
    Execute();
    Console.ReadKey();
}
/// <summary>
/// 连续相乘
/// </summary>
[Obsolete("此方法已经由 Math.Pow 代替")]
private static double CiMi(double p, int m)
{
    // NOTE: 微软有自带的算 X 的 Y 次方的函数,此处改成调用为佳,
    // 不要再大费周章了,所以此方法已废除
    return Math.Pow(p, m);
}
/// <summary>
/// 前一项的次幂数
/// </summary>
private static int MiShu(int[] m, bool isNext = false)
{
    // NOTE: 这里,索引应该从 0-5,而不是 1-6
    for (int i = 0; i < ArrayLength; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            // NOTE: MiShu1 和 MiShu2 之间的差别只是在于这里的判断,
            // 两者在此处 0 和 1 相互反了一下,对不对?
            // 写代码一定要尽量避免重复代码!能合并的地方就需要合并
            if (nArray[i] == Convert.ToInt32(isNext)) m[i] += 1;
        }
        for (int j = i + 1; j < ArrayLength; j++)


        {
            if (nArray[i] == Convert.ToInt32(isNext)) m[i] += 1;
        }
    }
    // NOTE: 当完成循环之后,i 一定是 ArrayLength - 1,所以此处,
    // 我觉得你是想获取最后一个元素的值,我觉得这里你应该这么做:
    return m[ArrayLength - 1];
}
private static double Max(double[] a, ref int m)
{
    // NOTE: 这个方法里面我完全没看懂你在干什么,
    // 你居然对着一个空的 P 数组进行操作,a 传进来干嘛的?
    // 另外,你这个传进来的 m 干嘛用的?我都没看懂,要修改入参
    // 必须使用 ref 或者 out 关键字
    // 代码改为:
    double max = a.Max();
    m = a.ToList().IndexOf(max);
    return max;
}
private static void Execute()
{
    // 概率最大组的序号
    int res = 0;
    double q = 0.000001;
    // 6 个开关发生故障的概率
    double[] p = new double[ArrayLength];
    for (int i = 0; i < ArrayLength; i++)
    {
        int[] m = new int[ArrayLength];
        int[] n = new int[ArrayLength];

        // NOTE: 你这里这么多括号,看着不晕吗?!
        p[i] = Math.Pow(q, MiShu(m)) * Math.Pow(1 - q, MiShu(n, true));
    }
    // NOTE: 你这里得到了 pmax,然后有没有用它,最后居然还输出了 res 变量,
    // 你到底要闹哪样?Form 上写的是 “输出PMax”,这里么输出 res,我一并改了
    double pmax = Max(p, ref res);
    Console.WriteLine("PMax 为: {0},概率最大组序号:{1}", pmax, res);
}

热点排行