简体   繁体   English

将2D数组从函数转换为main()中的变量

[英]Getting 2D array from a function into a variable in main()

I am a begginer in C#. 我是C#的入门者。 I have a problem that can be simply solved by you guys. 我有一个可以简单地解决的问题。 I have a function that creates 2D array with numbers and returns it. 我有一个函数,用数字创建2D数组并返回它。 I would like to use this function in main() to get this 2D array in main(). 我想在main()中使用此函数以在main()中获得此2D数组。 Here is a code that doesn't work (it prints "Y:" only). 这是无效的代码(仅显示“ Y:”)。 Could you please help me? 请你帮助我好吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Simulation
    {

        static int[,] Initialize(int x, int a, int b)
        {
            Random r = new Random();

            int[,] positions = new int[2,x];
            for(int i=0;i<x;i++){
                positions[0,i]=a*r.Next();
                positions[1,i]=b*r.Next();
            }
            return positions;
        }

        static void Main(string[] args)
        {
            int[,] array=Initialize(100,100,100);
            System.Console.WriteLine("Y:", array[1, 1]);
            System.Console.ReadKey();
        }
    }
}

Have a good day! 祝你有美好的一天!

您的输出行应为:

System.Console.WriteLine("Y: {0}", array[1, 1]);

A small problem at System.Console.WriteLine("Y:", array[1, 1]); System.Console.WriteLine("Y:", array[1, 1]);一个小问题 line, either use 线,无论使用

System.Console.WriteLine("Y: " + array[1, 1]);

or 要么

 System.Console.WriteLine("Y: {0}",  array[1, 1]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM