Возвращаемые значения с массивами - PullRequest
0 голосов
/ 04 ноября 2018

Я довольно новичок в программировании, и мне просто нужна помощь в том, что я делаю неправильно.

Это код, который у меня есть. Да, это для домашней работы, но я не понимаю, что мне делать дальше.

В методе CreateRandomlyFilledArray я должен создать выделенный массив. Этот метод будет принимать в качестве единственного параметра целое число. Затем внутри метода создается массив, заполненный значениями, которые были созданы методом случайным образом. (значения могут быть от 0 до 100).

Затем массив будет передан (в качестве параметра) методу PrintArray, который примет в качестве единственного параметра массив целых чисел и распечатает все в массиве.

class Returning_An_Array
{
    public void RunExercise()
    {
        ArrayReturnMethods m = new ArrayReturnMethods();
        int[] nums1; 
        nums1 = m.CreateRandomlyFilledArray(10);          

        m.PrintArray(nums1);
    }
}

class ArrayReturnMethods
{

    public int[] CreateRandomlyFilledArray( int size )
    {
        int[] newNums = new int[size];
        for (int value = 0; value < newNums.Length; value++)
        {
            return newNums;

        }
        return newNums;

    }

    public void Printarray( int[] value )
    {
        for(int i = 0; i < value.Length; i++)
        {
            Console.WriteLine("value is: {0}", value[i]);
        }
    }
}

Спасибо большое !!

1 Ответ

0 голосов
/ 04 ноября 2018

Не задавайте здесь домашнее задание. Особенно, когда немного чтения решит вашу проблему. Удачи с домашней работой. :)

class Program
{
    /*
    I assume you are trying to
    1. Create an array of integers
    2. Store random numbers (between 0 and 100) inside that array
    3. Print the numbers in the array 

    You have alot of reading to do as theres alot of fundemental mistakes in both your approach and code.
     */
    static void Main(string[] args)
    {

        // creating an array with random numbers
    ArrayMethods m = new ArrayMethods();
    int[] nums1; 
    nums1 = m.CreateRandomlyFilledArray(10);          

    m.Printarray(nums1);

    }

    class ArrayMethods
    {
        /*
             - First you have to fill the array with random numbers
        In your solution, you have created "CreateRandomlyFilledArray".
         1. You created the a new array of integers which is good
         2. The way you attempted to fill the new array is incorrect
        */
        public int[] CreateRandomlyFilledArray(int size)
        {
            int[] newNums = new int[size];

            Random numGen = new Random(); //  This will be used to generate random numbers
            for (int elementNum = 0; elementNum < newNums.Length; elementNum++)
            {
                // here we will put a random number in every position of the array using the random number generator
                newNums[elementNum] = numGen.Next(0, 100); // we pass in you minimum and maximum into the next function and it will return a random number between them
            }

            // here we will return the array with the random numbers
            return newNums;
        }

        /*
        - This function prints out each item in an integer array
            1. You do not need to a return value as you will not be returning any thing so, Use "void".
        */
        public void Printarray(int[] value)
        {
            for (int i = 0; i < value.Length; i++)
            {
                Console.WriteLine("value is: {0}", value[i]);
            }
        }

    }
}
...