Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 并非所有代码路径都返回一个";“价值”;_C# - Fatal编程技术网

C# 并非所有代码路径都返回一个";“价值”;

C# 并非所有代码路径都返回一个";“价值”;,c#,C#,您好,我正在尝试制作一个智囊团游戏,让用户猜测4-10之间的数字序列,而不是颜色,但出于某种原因,我的GetRandomNumberCount和我的GeneratorAndomNumber给了我一个错误,并非所有代码路径都返回一个值 如有任何指导,将不胜感激 public static int GetRandomNumberCount() { //Create the secret code Random RandomClass = new Random();

您好,我正在尝试制作一个智囊团游戏,让用户猜测4-10之间的数字序列,而不是颜色,但出于某种原因,我的GetRandomNumberCount和我的GeneratorAndomNumber给了我一个错误,并非所有代码路径都返回一个值

如有任何指导,将不胜感激

public static int GetRandomNumberCount()
{
      //Create the secret code
      Random RandomClass = new Random();

      int first = RandomClass.Next(1, 5);
      int second = RandomClass.Next(1,5);
      int third = RandomClass.Next(1,5);
      int forth = RandomClass.Next(1,5);

      Console.WriteLine ("You are playing with M@sterB@t");
      Console.WriteLine ("Bot Says : You Go First");
      Console.WriteLine("Game Settings ");
      Console.WriteLine("The Game Begins");
}

这是因为它们不返回值。例如,GetRandomNumberCount将
Int
设置为其返回类型,但没有返回语句。如果不想返回任何内容,请将返回类型设置为void。以此为例,

 public static int[] GetRandomNumberCount()
{
    //Create the secret code
    Random RandomClass = new Random();

    int first = RandomClass.Next(1, 5);
    int second = RandomClass.Next(1,5);
    int third = RandomClass.Next(1,5);
    int forth = RandomClass.Next(1,5);

    Console.WriteLine ("You are playing with M@sterB@t");
    Console.WriteLine ("Bot Says : You Go First");
    Console.WriteLine("Game Settings ");
    Console.WriteLine("The Game Begins");

    //This is where you would return a value, but in this case it seems you want to return an array of ints
    //Notice how I changed the return type of the method to Int[]
   int[] numbers = new int[4];
   numbers.Add(first);
   numbers.Add(second);
   numbers.Add(third);
   numbers.Add(fourth);

   //This is the actual return statement that your methods are missing
   return numbers;

    }
不管你是否真的想要返回一个int数组都是没有意义的,我只是在猜测。真正的好处是
int[]
in

public static int[] GetRandomNumberCount()

声明返回类型,这意味着您需要返回语句

您得到了这个错误,因为您的方法签名说它返回一个
int
,但您没有返回任何内容。我看到的是,由于您只是在打印行,所以您希望有一个带有
void
返回类型的方法,如下所示

public static void GetRandomNumberCount()
{
      //Create the secret code
      Random RandomClass = new Random();

      int first = RandomClass.Next(1, 5);
      int second = RandomClass.Next(1,5);
      int third = RandomClass.Next(1,5);
      int forth = RandomClass.Next(1,5);

      Console.WriteLine ("You are playing with M@sterB@t");
      Console.WriteLine ("Bot Says : You Go First");
      Console.WriteLine("Game Settings ");
      Console.WriteLine("The Game Begins");
}

只发布相关的代码,而不是你的整个项目。他显然想返回这些INT,否则这些INT是为注释而创建的,然后就退出了scope@psoshmo除非OP公司澄清,否则这只是目前的猜测。此外,你认为你将如何返回所有的随机数?在一个整数数组中,但你是对的,我在这里假设,我不应该这样做。