C sharp array error Windows Form Applicaton
I am trying to figure out what the error in this code is:
private void button2_Click(object sender, EventArgs e)
{
int[] teams = new int[10] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
temp = teams[2];
for (int i = 3; i <= 10; i++)
teams[i-1] = teams[i];
richTextBox1.Text = ("Round 1: "+teams[0]+","+teams[1]+" "+teams[2]+","+teams[9]+" "+teams[3]+","+teams[8]+" "+teams[4]+","+teams[7]+" "+teams[5]+","+teams[6]);
}
I am not getting any errors in the server explorer, but the program crashes when I debug and I get "IndexOutOfRangeException was unhandled"
The output is supposed to be Round: 1,2 3,10 4,9 5,8 6,7 Roun开发者_运维百科d: 1,3 4,2 5,10 6,9 7,8 Round: 1,4 5,3 6,2 7,10 8,9 etc... until all teams have played each other exactly once. Team 1 is fixed while the rest of the items in the array iterate.
Because index 10 does not really exist in your array teams
. Indexes are zero-based, so the valid indexes of teams
are 0-9. Your loop goes from 3 to 10, so your last iteration throws an exception.
精彩评论