-2

エラーは

インデックス範囲外の例外

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);
        }
    }
}

私はプログラミングに非常に慣れていないので、ここで少し迷っています。このコードは本からのものであり、本にはまだエラーがありません。エラーがそれほど複雑でないことを願っています。私はまだ大量のコードを入力し、コードを読んで理解しようとしているためです。

4

1 に答える 1

1

変数に対する束縛チェックはありませんj。明らかに、2 番目の配列要素のサイズを超えて増加しています。Servy が私たちのコメントで指摘したように、これはよくある 1 つのエラーによるものです。= を <= から削除するようにコードを調整しました。

for 行は次のようにする必要があります...

for (i = (int)start, j = 0; i < (int)end; i++, j++)
于 2013-09-23T15:03:45.830 に答える