我正在尝试创建一个C#for循环,它将循环遍历项目数组并打印用户的第一个日期.下面是数组的代码(我认为是正确的).
string[,] arrHolidays = new string[2, 3];
arrHolidays[0, 0] = "10/05/2015";
arrHolidays[0, 1] = "15/05/2015";
arrHolidays[0, 2] = "Danny";
arrHolidays[1, 0] = "20/05/2015";
arrHolidays[1, 1] = "22/05/2015";
arrHolidays[1, 2] = "Kieran";
Run Code Online (Sandbox Code Playgroud)
下面是For循环的代码(抛出错误)
for(int i = 0; i < arrHolidays.Length; i++)
{
Console.WriteLine(arrHolidays[i, 0]);
}
Run Code Online (Sandbox Code Playgroud)
索引超出范围异常未得到处理.这是我收到的错误.当它抛出错误并且我检查值1时它是2,如果这是解决它的任何帮助.我希望在控制台应用程序中看到的是:
10/05/2015
20/05/2015
INB4我几乎没有任何c#经验这是我的第一个项目,所以请解释任何帮助:)
我有以下代码从Listbox获取值.我选了三位老师.当'i'值达到3时,它显示' 索引超出了数组的范围 '错误
string selectedTeachers = Request["SelectedTeachersList"];
int[] teachers_ID = Array.ConvertAll(selectedTeachers.Split(','), int.Parse);
for (int i = 0; i <= teachers_ID.Length; i++)
{
int Id = teachers_ID[i];
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
List<string> group = new List<string>();
List<int> groupInNumber = new List<int>();
Dictionary<string, List<string>> dicMyMap = new Dictionary<string, List<string>>();
Dictionary<string, int[]> a = new Dictionary<string, int[]>();
Dictionary<string, string[]> b = new Dictionary<string, string[]>();
private void SetData(out ExpandableListViewAdapter mAdapter)
{
int[] currentStatus = a["currentStatus"];
string[] statusDesc = b["description"];
int[] ticketID = a["ticketID"];
foreach (int s in currentStatus)
{
if (s == 1)
{
group.Add(statusDesc[0]);
}
else if (s == 2)
{
group.Add(statusDesc[1]);
}
else if (s == 3)
{
group.Add(statusDesc[2]);
}
else …Run Code Online (Sandbox Code Playgroud) 我有一个程序,它返回与两个字符串中相同位置的两个不匹配字符的 ASCII 表的距离。我想缩短我的程序,但以这样一种方式返回与以前相同的结果。(程序不能返回负值,因此它检查每个 If 指令哪个数字具有更大的值并且字符串总是具有相同的长度) 现在程序每次在比较时都会抛出错误:“System.IndexOutOfRangeException: 'Index 超出了数组的边界。” 但它显示正确的结果..
代码
public static void incompatible(string a, string b)
{
char[] characters1 = new char[a.Length];
char[] characters2 = new char[b.Length];
for (int i = 0; i < a.Length; i++)
{
characters1 [i] = a[i];
}
for (int i = 0; i < b.Length; i++)
{
characters2 [i] = b[i];
}
if (a.Length >= 0 || b.Length >= 0)
{
if (characters1 [0] != characters2 [0])
{
if (characters1 [0] > characters2 [0]) …Run Code Online (Sandbox Code Playgroud) 未处理的异常:System.IndexOutOfRangeException:索引超出了数组的边界(在第一个if语句处)
static int arrayRows = 20;
static int arrayCols = 20;
public void printBoard()
{
int neighbours;
for (y = 0; y < arrayCols; y++)
for (x = 0; x < arrayRows; x++)
{
neighbours = 0;
// Count number of neighbours surrounding live cell
if (parentGen[x-1, y-1] == 1) // top left
neighbours++;
if (parentGen[x-1, y] == 1) // left
neighbours++;
if (parentGen[x-1, y+1] == 1) // bottom left
neighbours++;
if (parentGen[x, y-1] == 1) // middle top
neighbours++; …Run Code Online (Sandbox Code Playgroud) 我有以下数组:
public string reArrange(string s)
{
char[] array = s.ToCharArray();
int length = array.Length;
char[] arranged = new char[length];
for (int i = 0; i < length; i++)
{
int newposition = length - i;
arranged[newposition] = array[i];
}
return new string(arranged);
}
Run Code Online (Sandbox Code Playgroud)
但是上述方法会引发以下错误:
System.IndexOutOfRangeException: 索引超出数组范围。
那么可能有什么问题呢?
我的程序中有以下代码,它在行yearList.SetValue(years [count],count)中抛出索引超出绑定的异常;
protected void invoiceYear_DataBound(object sender, EventArgs e)
{
//invoiceYear.SelectedItem.Value= GetYearRange();
String[] years = GetYearRange().Split(new char[] { '[', ',', ']',' ' });
ListItem [] yearList = new ListItem[]{};
System.Diagnostics.Debug.WriteLine("years-->" + years.Length);
for (int i = 0; i < years.Length; i++)
{
System.Diagnostics.Debug.WriteLine("years-->" + years.GetValue(i));
}
int count = 0;
foreach (String str in years)
{
if (string.IsNullOrEmpty(str))
System.Diagnostics.Debug.WriteLine("empty");
else
{
yearList.SetValue(years[count], count);
count++;
}
}
//System.Diagnostics.Debug.WriteLine("yearList-->" + yearList.GetValue(0));
//invoiceYear.Items.AddRange(yearList);
}
Run Code Online (Sandbox Code Playgroud) 当我运行它时,它说索引超出了数组的范围,但事实并非如此.如果我改变公共静态ConectorRec [] ch = new ConectorRec [74]; to ConectorRec [] ch = new ConectorRec [75]; 那么它们并不是都引用了一个对象,而是在我的代码的其他部分抛出了其他错误.
using System;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using System.Collections.Generic;
namespace Dots
{
public static class Conectors
{
public static ConectorRec[] ch = new ConectorRec[74];
public static void intitialize()
{
ch[0] = new ConectorRec(new Rectangle(10, 20, 10, 40));
ch[1] = new ConectorRec(new Rectangle(20, 10, 40, 10));
ch[2] = new ConectorRec(new Rectangle(20, 60, 40, 10));
ch[3] = new ConectorRec(new Rectangle(60, 20, 10, 40));
int t …Run Code Online (Sandbox Code Playgroud) 我一直试图制作一个ludo游戏来学习更多关于C#的信息.不幸的是,我遇到了一个我无法弄清楚的for循环,它给了我一个出界的错误.我在这段代码中尝试做的是制作电路板(这是15x15阵列)我将游戏板上的所有空白点分配给值9,以便我以后可以轻松地使用它.
象限1执行它的目的,但它在象限2中给出了"出界"错误.
// Make the 2D array (Game board)
int[,] GameBoard = new int[15, 15];
// Quadrant 1
for(int Blankspotx1 = 0; Blankspotx1 < 7; Blankspotx1++)
{
GameBoard[Blankspotx1, 0] = 9;
int Blankspoty1 = 0;
for (Blankspoty1 = 0; Blankspoty1 < 7; Blankspoty1++)
{
GameBoard[Blankspotx1, Blankspoty1] = 9;
}
Blankspoty1 = 0;
}
// Quadrant 2
for (int Blankspotx2 = 10; Blankspotx2 < 16; Blankspotx2++)
{
GameBoard[Blankspotx2, 0] = 9;
int Blankspoty2 = 0;
for (Blankspoty2 = 0; Blankspoty2 < …Run Code Online (Sandbox Code Playgroud) using System;
using System.IO;
namespace _2._1
{
class Program
{
public struct nariai
{
public string vardas;
public string pavarde;
public double pinigai;
};
static void Main(string[] args)
{
StreamReader failas = new StreamReader("nariai.txt");
string a = failas.ReadLine();
int nariuKiekis = int.Parse(a);
nariai[] narys = new nariai[nariuKiekis];
string[] info = new string[nariuKiekis];
for (int i = 0; i < nariuKiekis; i++)
{
info[i] = failas.ReadLine();
string[] informacija = info[i].Split(' ');
narys[i].vardas = informacija[i];
narys[i].pavarde = informacija[i + 1];
narys[i].pinigai = …Run Code Online (Sandbox Code Playgroud)