C#方法错误:检测到无法访问的代码,并且并非所有代码路径都返回值”; 静态类Util { 静态随机运行=新随机(); 公共静态列表生成器(int num) { List ptList=新列表(); int count=num; for(int i=0;i
大家好 当我试图编译此代码时,出现以下错误:C#方法错误:检测到无法访问的代码,并且并非所有代码路径都返回值”; 静态类Util { 静态随机运行=新随机(); 公共静态列表生成器(int num) { List ptList=新列表(); int count=num; for(int i=0;i,c#,C#,大家好 当我试图编译此代码时,出现以下错误: 检测到无法访问的代码 并非所有代码路径都返回值 我不明白为什么会发生这种事。。。任何帮助都将不胜感激。 谢谢 函数应返回列表。将其返回到循环之外 static class Util { static Random ran = new Random(); public static List<Point3d> RandomptGenerator(int num) { List <Point3d
谢谢 函数应返回
列表
。将其返回到循环之外
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;
}
}
}
}
List ptList=new List();
int count=num;
for(int i=0;i
您的函数应该返回列表
。将其返回到循环之外
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;
}
}
}
}
List ptList=new List();
int count=num;
for(int i=0;i
在代码中,您将在for循环中返回ptList。如果计数为0,并且您永远不会进入循环,会发生什么
移动返回列表;循环外部。在代码中,您将返回for循环内部的ptList。如果计数为0,并且您永远不会进入循环,会发生什么
移动返回列表;循环外部。该函数应返回Point3d对象列表。但是,不能保证for循环条件为真(i
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;
公共静态列表生成器(int num)
{
List ptList=新列表();
int count=num;
for(int i=0;i
该函数应返回Point3d对象的列表。但是,不能保证for循环条件为真(iList<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;
公共静态列表生成器(int num)
{
List ptList=新列表();
int count=num;
for(int i=0;i
如果您想生成num
随机点(请注意,在当前代码中您创建了num*num
点),您所要做的就是
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;
}
静态类Util
{
静态随机运行=新随机();
公共静态列表生成器(int num)
{
//创建列表。。。
List ptList=新列表();
//…用值填充它。。。
for(int i=0;i
通常,我们使用Linq为我们生成列表:
static class Util
{
static Random ran = new Random();
public static List<Point3d> RandomptGenerator(int num)
{
// Create list...
List <Point3d> ptList = new List<Point3d>();
// ...fill it with values...
for (int i = 0;i < count; i++)
ptList.Add(new Point3d(ran.Next(0, 40), ran.Next(0, 30), 0.0));
// ...return the list
return ptList;
}
}
使用System.Linq
...
静态类Util
{
静态随机运行=新随机();
公共静态列表生成器(int num)
{
返回可枚举
.范围(0,num)
.Select(=>newpoint3d(ran.Next(0,40),ran.Next(0,30),0.0))
.ToList();
}
}
如果您想生成num
随机点(请注意,在当前代码中您创建了num*num
点),您所要做的就是
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;
}
静态类Util
{
静态随机运行=新随机();
公共静态列表生成器(int num)
{
//创建列表。。。
List ptList=新列表();
//…用值填充它。。。
for(int i=0;i
通常,我们使用Linq为我们生成列表:
static class Util
{
static Random ran = new Random();
public static List<Point3d> RandomptGenerator(int num)
{
// Create list...
List <Point3d> ptList = new List<Point3d>();
// ...fill it with values...
for (int i = 0;i < count; i++)
ptList.Add(new Point3d(ran.Next(0, 40), ran.Next(0, 30), 0.0));
// ...return the list
return ptList;
}
}
使用System.Linq
...
静态类Util
{
静态随机运行=新随机();
公共静态列表生成器(int num)
{
返回可枚举
.范围(0,num)
.Select(=>newpoint3d(ran.Next(0,40),ran.Next(0,30),0.0))
.ToList();
}
}
如果“count”为0,它甚至不会在第1位。因为i=0,并且i如果“count”为0,它甚至不会在第1位。因为i=0和i应该是:
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;
}
公共静态列表生成器(int num)
{
List ptList=新列表();
int count=num;
for(int i=0;istatic Random ran = new Random();
public static List<Point3d> RandomptGenerator(int num)
{
var <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;
}