for循环抛出范围异常

时间:2013-09-23 13:44:23

标签: c# exception runtime-error indexoutofboundsexception

错误是

  

索引超出范围异常

并且位于for循环的第一行:

for (i = (int)start, j = 0; i <= (int)end; i++, j++)
{  // Since linear relationships...
    idealWeights[0, j] = (female += 3.5);   **ERROR HERE ON THIS LINE**
    idealWeights[1, j] = (male += 4.0);
}

txtStart和txtEnd中的值为: txtStart = 36 txtEnd = 96

以下是代码:

using System;
using System.Windows.Forms;

public class frmMain : Form
{
    private TextBox txtStart;
    private TextBox txtEnd;
    private ListBox lstResults;
    private Button btnClose;
    private Label label1;
    private Label label2;
    private Button btnCalc;

    private void InitializeComponent()
    {
            this.btnCalc = new System.Windows.Forms.Button();
            this.txtStart = new System.Windows.Forms.TextBox();
            this.txtEnd = new System.Windows.Forms.TextBox();
            this.lstResults = new System.Windows.Forms.ListBox();
            this.btnClose = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // btnCalc
            // 
            this.btnCalc.Location = new System.Drawing.Point(26, 197);
            this.btnCalc.Name = "btnCalc";
            this.btnCalc.Size = new System.Drawing.Size(75, 23);
            this.btnCalc.TabIndex = 0;
            this.btnCalc.Text = "Ca&lculate";
            this.btnCalc.UseVisualStyleBackColor = true;
            this.btnCalc.Click += new System.EventHandler(this.btnCalc_Click);
            // 
            // txtStart
            // 
            this.txtStart.Location = new System.Drawing.Point(172, 12);
            this.txtStart.Name = "txtStart";
            this.txtStart.Size = new System.Drawing.Size(100, 20);
            this.txtStart.TabIndex = 1;
            // 
            // txtEnd
            // 
            this.txtEnd.Location = new System.Drawing.Point(172, 38);
            this.txtEnd.Name = "txtEnd";
            this.txtEnd.Size = new System.Drawing.Size(100, 20);
            this.txtEnd.TabIndex = 2;
            // 
            // lstResults
            // 
            this.lstResults.FormattingEnabled = true;
            this.lstResults.Location = new System.Drawing.Point(26, 96);
            this.lstResults.Name = "lstResults";
            this.lstResults.Size = new System.Drawing.Size(246, 95);
            this.lstResults.TabIndex = 3;
            // 
            // btnClose
            // 
            this.btnClose.Location = new System.Drawing.Point(197, 197);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(75, 23);
            this.btnClose.TabIndex = 4;
            this.btnClose.Text = "&Close";
            this.btnClose.UseVisualStyleBackColor = true;
            // 
            // label1
            // 
            this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label1.Location = new System.Drawing.Point(23, 12);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(143, 20);
            this.label1.TabIndex = 5;
            this.label1.Text = "Start:";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label2
            // 
            this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label2.Location = new System.Drawing.Point(23, 38);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(143, 20);
            this.label2.TabIndex = 6;
            this.label2.Text = "End:";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // frmMain
            // 
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.btnClose);
            this.Controls.Add(this.lstResults);
            this.Controls.Add(this.txtEnd);
            this.Controls.Add(this.txtStart);
            this.Controls.Add(this.btnCalc);
            this.Name = "frmMain";
            this.ResumeLayout(false);
            this.PerformLayout();

    }

    public frmMain()
    {
        InitializeComponent();
    }

    public static void Main()
    {
        frmMain main = new frmMain();
        Application.Run(main);
    }

    const double MININCHES = 36;
    const double MAXINCHES = 96;

    private void btnCalc_Click(object sender, EventArgs e)
    {
        bool flag;
        int i;
        int j;
        double start;
        double end;
        double male;
        double female;
        double[,] idealWeights;
        string buff;

        //============================== Input ===============================================
        flag = double.TryParse(txtStart.Text, out start);  // Table start
        if (flag == false)
        {
            MessageBox.Show("Numeric only.");
            txtStart.Focus();
            return;
        }

        flag = double.TryParse(txtEnd.Text, out end);  // Table end
        if (flag == false)
        {
            MessageBox.Show("Numeric only.");
            txtEnd.Focus();
            return;
        }

        //============================= Validate input ==============================================
        if (start < MININCHES || start > MAXINCHES)  // Check table limits
        {
            MessageBox.Show("Table can only span " + MININCHES.ToString() + " to " +
                MAXINCHES + " inches.");
            txtStart.Focus();
            return;
        }
        if (end < MININCHES || end > MAXINCHES)
        {
            MessageBox.Show("Table can only span " + MININCHES.ToString() +
                " to " + MAXINCHES + " inches.");
            txtEnd.Focus();
            return;
        }
        if (end <= start)       // Can we display anything
        {
            MessageBox.Show("Starting value must be less than ending value");
            txtStart.Focus();
            return;
        }
        // Define the array for table data
        idealWeights = new double[2, (int)(end - start) + 1];

        //=============================== Process ====================================================
        start--;                // This is the new line
        female = 3.5 * start - 108;  // Set initial table values
        male = 4.0 * start - 128;

        for (i = (int)start, j = 0; i <= (int)end; i++, j++)
        {  // Since linear relationships...
            idealWeights[0, j] = (female += 3.5);
            idealWeights[1, j] = (male += 4.0);
        }
        //================================ Display step ==============================================
        for (i = (int)start, j = 0; i <= (int)end; i++, j++)
        {
            buff = String.Format("{0,5}{1,15}{2,15}", i, idealWeights[0, j], idealWeights[1, j]);
            lstResults.Items.Add(buff);
        }
    }
}

我对编程很新,所以我在这里有点迷失,这段代码来自一本书,书中还没有任何错误。所以希望错误不是很复杂,因为我只是输入很多代码并阅读代码来试图理解它。

1 个答案:

答案 0 :(得分:1)

你没有对j变量进行过资格检查。它显然超过了第二个数组元素的大小。正如Servy在我们的评论中指出的那样,这是一个常见的错误。我已调整代码以从&lt; =。

中删除=

for line需要看起来像......

for (i = (int)start, j = 0; i < (int)end; i++, j++)