指数数组的边界之外 ! [英] Index was outside the bounds of the array !

查看:83
本文介绍了指数数组的边界之外 !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
using System.IO;
using System.Reflection;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        bool khoa;
        bool check = new bool();

        Button[] Radombutton = new Button[7];
        Image[] RadomImage = new Image[7];
        int ClicksMouse;
        int[] Checks = new int[3];
        int[] List1 = new int[7];
        int[] List2 = new int[7];
        private void Form2_Load(object sender, EventArgs e)
        {
            Radombutton[1] = button1;
            Radombutton[2] = button2;
            Radombutton[3] = button3;
            Radombutton[4] = button4;
            Radombutton[5] = button5;
            Radombutton[6] = button6;
            NewGame();
        }
        public void LoadRadombutton()
        {
            for (int i = 1; i <= 6; i++)
            {
                Radombutton[i].Visible = true;
            }
        }

        public void Image()
        {

            RadomImage[List1[1]] = WindowsFormsApplication1.Properties.Resources.Q;
            RadomImage[List1[2]] = WindowsFormsApplication1.Properties.Resources.Q;
            List2[List1[1]] = 1;
            List2[List1[2]] = 1;

            RadomImage[List1[3]] = WindowsFormsApplication1.Properties.Resources.U;
            RadomImage[List1[4]] = WindowsFormsApplication1.Properties.Resources.U;
            List2[List1[3]] = 2;
            List2[List1[4]] = 2;

            RadomImage[List1[5]] = WindowsFormsApplication1.Properties.Resources.Y;
            RadomImage[List1[6]] = WindowsFormsApplication1.Properties.Resources.Y;
            List2[List1[5]] = 3;
            List2[List1[6]] = 3;


        }
        public void NewGame()
        {
            Random rnd = new Random();
            for (int i = 1; i <= 6; i++)
            {
                // RandomButton is a collection, right?
                Radombutton[i].Image = null;
            }
            for (int i = 1; i <= 6; i++)
            {
                bool checkSuccessful = true;
                int randomNumber = 6 * rnd.Next() + 1;
                // Kiểm tra trùng này !
                for (int j = 1; j < i - 1; j++)
                {
                    // Also List1 is a collection?
                    if (randomNumber == List1[j])
                    {
                        // Never use "GOTO"! use a variable to check.
                        checkSuccessful = false;
                        // Not nice but this is the only short solution I found for.
                        break;
                    }
                }
                // Check if the inner-loop-check was not failing.
                if (checkSuccessful)
                {
                    List1[i] = randomNumber;
                }
            }
            Image();
            LoadRadombutton();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            if (ClicksMouse == 1)
                return;

            if (List2[Checks[1]] == List2[Checks[2]])
            {
                Radombutton[Checks[2]].Visible = false;
                Radombutton[Checks[1]].Visible = false;
                int i = 0;
                bool Match = true;
                for (i = 1; i <= 6; i++)
                {
                    if (Radombutton[i].Visible == true)
                    {
                        Match = false;
                        break; // TODO: might not be correct. Was : Exit For
                    }
                }
                if (Match == true)
                {
                    MessageBox.Show(" ho ho ho");
                }
            }
            else
            {
                Radombutton[Checks[1]].Image = null;
                Radombutton[Checks[2]].Image = null;
            }
            Checks[1] = 0;
            Checks[2] = 0;
            ClicksMouse = 0;
        }

        private void button6_Click(object sender, EventArgs e)
        {

            if (khoa == false)
                return;

            if (timer1.Enabled == true)
                return;

            if (Checks[1] == 6)
                return;

            ClicksMouse += 1;
            Checks[ClicksMouse] = 6;
            button6.Image = RadomImage[6];

            if (ClicksMouse == 2)
                timer1.Enabled = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (khoa == false)
                return;

            if (timer1.Enabled == true)
                return;

            if (Checks[1] == 5)
                return;

            ClicksMouse += 1;
            Checks[ClicksMouse] = 5;
            button5.Image = RadomImage[5];


            if (ClicksMouse == 2)
                timer1.Enabled = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (khoa == false)
                return;

            if (timer1.Enabled == true)
                return;

            if (Checks[1] == 4)
                return;

            ClicksMouse += 1;
            Checks[ClicksMouse] = 4;
            button4.Image = RadomImage[4];
            if (ClicksMouse == 2)
                timer1.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {

            if (khoa == false)
                return;

            if (timer1.Enabled == true)
                return;

            if (Checks[1] == 3)
                return;

            ClicksMouse += 1;
            Checks[ClicksMouse] = 3;
            button3.Image = RadomImage[3];
            if (ClicksMouse == 2)
                timer1.Enabled = true;


        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (khoa == false)
                return;
            if (timer1.Enabled == true)
                return;
            if (Checks[1] == 2)
            {
                return;
            }
            ClicksMouse += 1;
            Checks[ClicksMouse] = 2;
          button2.Image = RadomImage[2];
            if (ClicksMouse == 2)
            {
               timer1.Enabled = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (khoa == false)
                return;
            if (timer1.Enabled == true)
                return;
            if (Checks[1] == 1)
                return;
            ClicksMouse += 1;
            Checks[ClicksMouse] = 1;
            button1.Image = RadomImage[1];
            if (ClicksMouse == 2)
                timer1.Enabled = true;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            khoa = true;
            NewGame();
           button7.Enabled = false;
           button7.Text = " Th?y th? nào :) ";
           check = true;
        }
    }
}




请帮我Pr0s!
错误:索引超出数组的范围!
在此:WindowsFormsApplication1.Properties.Resources.Q;
T_T帮帮我!



这是我的专案!
http://www.mediafire.com/?9bhl42t6wug27a4 [




Pls help me Pr0s !
Error :Index was outside the bounds of the array !
In this :WindowsFormsApplication1.Properties.Resources.Q;
T_T help me pls !



This is my Project !
http://www.mediafire.com/?9bhl42t6wug27a4[^]

推荐答案

我没有指出错误,但是很明显,在基于零的数组上运行基于1个基的循环是不正确的.代码还有很多其他问题,大多数是系统性的.

我认为将此类代码推到可行状态并不是没有意义的.一切都是硬编码的,随机命名的,依此类推.没办法,没关系.这不值得.我宁愿从头开始.有什么问题吗?

对不起.

—SA
I don''t to pin-point the bug, but it''s pretty much apparent that running 1-base loops on zero-based arrays cannot be correct. Code has many other problems, mostly methodical.

I don''t think pushing such code to workable state is pointless. Everything is hard-coded, randomly named and so on. No method, no care. It does not worth it. I would rather start from scratch. Any questions?

Sorry.

—SA


我认为您的问题是在其中没有调用 timer.start() 上面的代码.以加载形式或在某处调用所述方法,然后代替使用timer1.enable和禁用,使用stop and start方法
I think your problem is there is no call to timer.start() in the code above. Call the said method in form load or somewhere and then instead of using timer1.enable and disable use stop and start method


这篇关于指数数组的边界之外 !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆