并非所有代码路径都返回“值”

嗨,我想做一个策划游戏,我让用户猜测4-10而不是颜色之间的数字序列但由于某种原因我的GetRandomNumberCount和我的GenerateRandomNumber给我错误,并非所有代码路径都返回一个值。

任何指导将不胜感激

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设置为其返回类型,但没有return语句。 如果要返回任何内容,则将返回类型设置为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() 

声明一个返回类型意味着你需要一个return语句。

你得到的错误导致你的方法签名说它返回一个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"); }