【问题标题】:Return Values With Arrays带数组的返回值
【发布时间】:2018-11-04 02:34:42
【问题描述】:

我对编程很陌生,我只需要帮助解决我做错的事情。

这是我到目前为止的代码。是的,这是为了家庭作业,但我对接下来要做什么感到困惑。

在 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]);
        }
    }
}

非常感谢!!

【问题讨论】:

  • 您可能希望查看 System.Random 类。考虑如何在 CreateRandomlyFilledArray 函数的循环中使用它来设置数组中的值。此外,您的 Printarray 功能不会按照您的想法进行,您不想要。考虑将数组索引放在数组中作为 WriteLine 的参数 (Console.WriteLine("{0}", value[I]);)
  • 谢谢您,我根据您的理解进行了编辑!返回时仍然会出错。

标签: c# arrays visual-studio parameters return-value


【解决方案1】:

避免在这里问作业问题。尤其是当一些阅读可以解决您的问题时。祝你的家庭作业好运。 :)

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

    }
}

【讨论】:

  • 非常感谢!我会仔细阅读您的建议。我必须像程序员一样思考,因为我不擅长。
猜你喜欢
  • 2023-02-06
  • 1970-01-01
  • 2019-01-19
  • 2022-01-18
  • 1970-01-01
  • 2021-05-13
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多