not all code paths return a “value”

后端 未结 2 918
逝去的感伤
逝去的感伤 2021-01-29 15:47

Hi I am trying to make a mastermind game where I having the user guess a number sequence between 4-10 instead of colours but for some reason my GetRandomNumberCount and my Gener

相关标签:
2条回答
  • 2021-01-29 15:58

    That's because they don't return a value. For example GetRandomNumberCount has Intset as its return type, but has no return statement. If you want to return nothing set the return type to void. Using that as an example

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

    regardless of whether you actually want to return an int array is moot though, Im only guessing. The real take away is that the int[] in

    public static int[] GetRandomNumberCount()
    

    declares a return type meaning you need a a return statement.

    0 讨论(0)
  • 2021-01-29 16:23

    You are getting that error cause your method signature says it returns a int but your are not returning anything. What I see is, you meant to have a method with void return type like below since you are just printing the lines

    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");
    }
    
    0 讨论(0)
提交回复
热议问题