C#保龄球计算器。返回null的类数组

时间:2016-05-15 06:32:00

标签: c# arrays visual-studio null

我正在c#visual studio上开展一个项目,我正在尝试制作一个保龄球计算器。我为包含throw1和throw2的帧创建了一个类。我首先用一个按钮填充类的数组,并在游戏完成后计算得分。当我去计算分数时,它告诉我我有空值。下面是我的表格代码  类帧     {         public int intThrow1;         public int intThrow2;     }

主要表格是

public partial class Form1 : Form
{
    frames[] frame = new frames[11];

    public Form1()
    {


        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

        int intFrst = int.Parse(textBox1.Text);
        int intScnd = int.Parse(textBox2.Text);
        if (lbxScorecard.Items.Count <= 21)
        {
            int intIndx = 0;
            if (intFrst == 10)
            {
                frame[intIndx] = new frames();
                frame[intIndx].intThrow1 = intFrst;
                frame[intIndx].intThrow2 = 0;
                lbxScorecard.Items.Add(frame[intIndx].intThrow1);
                lbxScorecard.Items.Add(frame[intIndx].intThrow2);
                intIndx += 2;
            }
            else
            {
                frame[intIndx] = new frames();
                frame[intIndx].intThrow1 = intFrst;
                frame[intIndx].intThrow2 = intScnd;
                lbxScorecard.Items.Add(frame[intIndx].intThrow1);
                lbxScorecard.Items.Add(frame[intIndx].intThrow2);
                intIndx++;

            }
        }
        else
        {
            MessageBox.Show("max throws");
        }




    }

    private void button2_Click(object sender, EventArgs e)
    {

        int intScore = 0;

        for (int index = 0; index <= 11; index++)
        {

            if (frame[index].intThrow1 == 10 && index < 9)  // here is where it throws a null exception stating the index may be empty
            {
                if (frame[index + 1].intThrow1 != 10)
                {
                    intScore = intScore + frame[index].intThrow1 + frame[index + 1].intThrow1 + frame[index + 1].intThrow2;
                }
                else if (frame[index].intThrow1 == 10)
                {
                    intScore = intScore + frame[index].intThrow1 + frame[index + 1].intThrow1 + frame[index + 2].intThrow1;
                }
            }
            else if (frame[index].intThrow1 + frame[index].intThrow2 == 10 && index < 9)     //it was throwing the same exception here until i added && index<9 in the if statements.
            {
                intScore = intScore + frame[index].intThrow1 + frame[index].intThrow2 + frame[index + 1].intThrow1;
            }
            else 
            {
                intScore = intScore + frame[index].intThrow1 + frame[index].intThrow2;
            }

        }
        MessageBox.Show(intScore.ToString());
    }
}

1 个答案:

答案 0 :(得分:0)

问题是:

frames[] frame = new frames[11];

您还需要像在button1_Click函数中那样实例化数组的每个元素:

frame[intIndx] = new frames();

另请注意,您有一个类变量framebutton2_Click中的同一个变量,这可能是一个坏主意。