好的,我尝试了很多次自己的代码,但IDE-Visual Studio总是说它错了..
指数在界外错误之外
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//declare Arrays for buttons and
//LEDs
int[] buttonArray = new int[4]; // Left to right .-.. ------
//left, top, bottom, right
int[,] ledArray = new int[10, 10];
//declare variables
int strandSnake = 0;
int requestR = 0;
int countDeclareButtonArray = 0;
int countWriteButtonArray = 0;
//code
if (strandSnake != 1)
{
}
//Declaring the four buttons
for (int ip = 0; ip < 4; ip++)
{
buttonArray[countDeclareButtonArray] = countDeclareButtonArray;
countDeclareButtonArray++;
}
// writing the four buttons to the screen.
foreach (int ip in buttonArray)
{
requestR = buttonArray[countWriteButtonArray];
countWriteButtonArray++;
}
Console.ReadLine();
}
}
}
答案 0 :(得分:0)
你应该知道C#中的数组的索引范围来自 0 .. array.Length - 1
所以,如果你有
int[] a = new int[4];
您可以访问 a [0]。 a [1],a [2],a [3] ,
但是当你访问 a [4] 时,你会得到 INDEX在边界错误之外。
调试它并搜索错误来自哪里
答案 1 :(得分:0)
请查看代码段,countWriteButtonArray
变量的值为4
,因此您必须将0
分配给countWriteButtonArray
。
countWriteButtonArray=0;
foreach (int ip in buttonArray)
{
requestR = buttonArray[countWriteButtonArray];
countWriteButtonArray++;
}
如果您想从buttonArray
数组中获取元素,则可以使用:
foreach (int ip in buttonArray)
{
//
}
使用for循环使用索引迭代数组。
for(countWriteButtonArray=0;countWriteButtonArray<=buttonArray.GetUpperBound(0);countWriteButtonArray++)
{
requestR = buttonArray[countWriteButtonArray];
}