C# 未处理的异常。索引超出范围。必须为非负数且小于集合的大小。参数名称:索引
当我试图在程序中射击彩弹时,我遇到了上述问题。下面是类别(彩弹)代码C# 未处理的异常。索引超出范围。必须为非负数且小于集合的大小。参数名称:索引,c#,C#,当我试图在程序中射击彩弹时,我遇到了上述问题。下面是类别(彩弹)代码 class Paintballs { public List<Point> myClick; public Paintballs() { myClick = new List<Point>(); } public void add(Point location) { myClick.Add(loc
class Paintballs
{
public List<Point> myClick;
public Paintballs()
{
myClick = new List<Point>();
}
public void add(Point location)
{
myClick.Add(location);
}
public void paint(Graphics g)
{
foreach (Point p in myClick)
{
g.FillEllipse(Brushes.Blue, p.X, p.Y, 20, 20);
}
}
public Point getPoints(int hit)
{
return myClick[hit];
}
}
在表1中,问题似乎出现在picturebox1\u鼠标单击事件中。count
变量我不确定应该将其保持在0还是1。请让我知道。多谢各位
计数++;
Point shotHit=pball.getPoints(计数)
这是错误的
您必须在pball.getPoints(count-1)中传递count-1
或
在获得shotHit后递增问题是在调用pball.getPoints(count)之前递增计数。让我们了解一下您的程序在首次与之交互时的功能:
将触发picturebox1\u鼠标单击事件
点的pball列表添加了一个元素,现在正好有一个元素(在索引0处)与鼠标位置的点
计数从0增加到1
您的程序运行pball.getPoints(count);但计数是1。PBall点列表只有一个元素,它位于索引0中。正在调用myClick[1],但您正在查找myClick[0],即刚才添加的点
您可以在调用pball.getPoints(count)后增加count,也可以使用pball.getPoints(count-1)传入真正的索引值 问题出现在以下情况下。假设我们有一个列表
//创建字符串类型的列表
List<string> lstString = new List<string>();
但是如果你写
lstString[3]
它会引发异常,因为您传递了列表中未出现的索引。或者换句话说,索引编号大于字符串中的索引
这正是你在这里做的
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
pBalls.add(e.Location); // you add element at index 0
pictureBox1.Refresh();
count++; // count++ mean count = 1;
// now you passing 1 but the list only has zero index right.
Point shotHit = pBalls.getPoints(count);
if (ptrEinstein.Location == shotHit)
{
stopwatch.Stop();
MessageBox.Show("It took " + count + " shots and " + stopwatch.Elapsed + " seconds to hit the target");
}
}
解决方案:
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
pBalls.add(e.Location); // you add element at index 0
pictureBox1.Refresh();
Point shotHit = pBalls.getPoints(count);
if (ptrEinstein.Location == shotHit)
{
stopwatch.Stop();
MessageBox.Show("It took " + count + " shots and " + stopwatch.Elapsed + " seconds to hit the target");
}
count++; // increment at the end of the method.
}
在获得射击命中后尝试递增计数。似乎您总是试图获得比数组大小大一的索引。除此之外,在这个特定的示例中,您可以对List使用Last()方法。感谢您更新结果。如果有效,请将其标记为已回答
lstString[3]
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
pBalls.add(e.Location); // you add element at index 0
pictureBox1.Refresh();
count++; // count++ mean count = 1;
// now you passing 1 but the list only has zero index right.
Point shotHit = pBalls.getPoints(count);
if (ptrEinstein.Location == shotHit)
{
stopwatch.Stop();
MessageBox.Show("It took " + count + " shots and " + stopwatch.Elapsed + " seconds to hit the target");
}
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
pBalls.add(e.Location); // you add element at index 0
pictureBox1.Refresh();
Point shotHit = pBalls.getPoints(count);
if (ptrEinstein.Location == shotHit)
{
stopwatch.Stop();
MessageBox.Show("It took " + count + " shots and " + stopwatch.Elapsed + " seconds to hit the target");
}
count++; // increment at the end of the method.
}