2017-08-17 76 views
1
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; 
      } 
     } 
    } 
} 

傢伙嗨,C#方法錯誤:檢測不到的代碼,而不是所有的代碼路徑返回一個值」

我收到以下錯誤,當我嘗試編譯這段代碼:

  1. 無法訪問代碼檢測
  2. 並非所有的代碼路徑返回一個值

我無法弄清楚這是爲什麼HAP pening ...任何幫助將不勝感激。 謝謝!

+3

'返回ptList;'也許應該是在方法 –

+0

看看那裏你return語句的結束 –

回答

1

您的函數應返回List<Point3d>。返回它在循環外

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; 
3

在你的代碼裏面有返回ptList for循環。如果count爲0,將會發生什麼情況,並且您將永遠不會進入循環。

move return ptList;循環之外。

2

函數應該返回一個Point3d對象列表。但是不能保證for循環的條件是成立的(我的數字爲<),並且完全可以達到return語句。這就是編譯器抱怨的原因。如果你想生成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; 
    } 
0

,所有你必須:

您應該移動的return語句,循環外如下做的是

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; 
    } 
} 

通常情況下,我們使用的LINQ以便爲我們生成列表:

using System.Linq 

... 

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

    public static List<Point3d> RandomptGenerator(int num) 
    { 
     return Enumerable 
     .Range(0, num) 
     .Select(_ => new Point3d(ran.Next(0, 40), ran.Next(0, 30), 0.0)) 
     .ToList(); 
    } 
} 
0

如果「count」是0,它甚至不會在第一次進入。因爲我= 0和我< 0是錯誤的。

嘗試這樣的:

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; 
    } 
0

應該是:

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; //<------Must be here! 
    } 
0

與您的代碼的問題是for循環。

它不會達到您的回報價值。

如果您的值小於零。

Ex。

count =0; 
for (int i = 0;i < count;i++) // the loop will end. when count is zero. 

同樣的,在你的內循環:

count =0; 
for (int j = 0;j < count;j++) // the loop will end. when count is zero. 

所以這就是爲什麼你遇到一個錯誤

可達代碼檢測,並不是所有的代碼路徑返回一個值

您可以使用此代碼修復:

static 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; 
} 
0

完美,返回ptList;處於錯誤的位置...解決了這個問題,顯然這是有道理的。

謝謝大家對你的那種回答

相關問題