gpt4 book ai didi

C# 方法错误 : Unreachable code detected and not all code paths return a value”

转载 作者:太空宇宙 更新时间:2023-11-03 18:55:48 25 4
gpt4 key购买 nike

static class Util
{
static Random ran = new Random();

public static List<Point3d> RandomptGenerator(int num)
{
List <Point3d> ptList = new List<Point3d>();
int count = num;

for (int i = 0;i < count;i++)
{
for (int j = 0;j < count;j++)
{
double x = i;
double y = j;

x = ran.Next(0, 40);
y = ran.Next(0, 30);

ptList.Add(new Point3d(x, y, 0.0));

return ptList;
}
}
}
}

大家好,

当我尝试编译这段代码时出现以下错误:

  1. 检测到无法访问的代码
  2. 并不是所有的代码路径都有返回值

我不明白为什么会这样...任何帮助将不胜感激。谢谢!

最佳答案

在您的代码中,您将在 for 循环内返回 ptList。如果 count 为 0 并且您永远不会进入循环,将会发生什么情况。

移动返回点列表;循环外。

关于C# 方法错误 : Unreachable code detected and not all code paths return a value”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45730285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com