创建由星号组成的多维数组

时间:2014-09-08 21:45:27

标签: c# arrays multidimensional-array dynamic

我是编程新手,我希望我的程序运行一个由星号组成的表(星号)。例如。表4x3有4x3星。但我最初的问题是我不知道如何以这样的方式实现多维数组,我只需要更改行和列的初始值,以便创建更多或更少的星。

所以:这是我目前的代码:

using System;

namespace ConsoleApplication1
{
    class Multidimensional_array
    {
        static void Main(string[] args)
        {
            int arrayRows = 4;
            int arrayCols = 3;

            int[,] arrayTimes;
            arrayTimes = new int [arrayRows, arrayCols];
            //String star = "*";

            for( int i = 0; i <= arrayRows; i++) {
                for( int j = 0; j <= arrayCols; j++) {
                    //Console.WriteLine("*");
                    //arrayTimes[i, j] = Convert.ToInt32(Console.ReadLine());
                }

            }
            Console.ReadLine();
        }

    }
}

所以我只想要,如果我将int arrayRows更改为5并将int arrayCols更改为5,那么我会收到一个5x5的星表。 Ť

3 个答案:

答案 0 :(得分:1)

我认为你非常接近,你只是为数组使用了错误的数据类型,没有将*分配给所述数组中的每个位置,而你当前的代码会给你一个{ {1}}例外。您可能有4行3列,但数组索引从零开始,这意味着当您访问位置1,2,3等时,分别使用索引0,1,2等。

因此,由于您要存储文本“*”,因此应使用ArrayIndexOutOfBoundschar[,]作为多维数组。我为此选择了string[,]

char[,]

答案 1 :(得分:0)

简单的解决方案

static void Main(string[] args)
        {
            // Get the number of rows
            Console.WriteLine("Enter the number of rows:");
            int arrayRows = Convert.ToInt32(Console.ReadLine());

            // Get the number of columns
            Console.WriteLine("Enter the number of columns:");
            int arrayCols = Convert.ToInt32(Console.ReadLine());

            // For each item in the row
            for (int i = 0; i < arrayRows; i++)
            {
                // For each item in the column
                for (int j = 0; j < arrayCols; j++)
                {
                    // Show a star
                    Console.Write("*");
                }

                // End the line
                Console.WriteLine(""); 
            }

            // Read the line to stop the app from closing
            Console.ReadLine();
        }

如果你想存储星星

    static void Main(string[] args)
    {
        // Get the number of rows
        Console.WriteLine("Enter the number of rows:");
        int arrayRows = Convert.ToInt32(Console.ReadLine());

        // Get the number of columns
        Console.WriteLine("Enter the number of columns:");
        int arrayCols = Convert.ToInt32(Console.ReadLine());

        // Create an array
        char[,] arrayTimes = new char[arrayRows, arrayCols];
        char star = '*';

        // For each item in the row
        for (int i = 0; i < arrayRows; i++)
        {
            // For each item in the column
            for (int j = 0; j < arrayCols; j++)
            {
                // Show a star
                arrayTimes[i, j] = star;
            }
        }

        // Read the line to stop the app from closing
        Console.ReadLine();
    }

答案 2 :(得分:0)

这样做你想要的,

        int arrayRows = 2;
        int arrayCols = 2;

        char[,] arrayTimes;
        arrayTimes = new char[arrayRows, arrayCols];
        //String star = "*";

        for (int i = 0; i < arrayRows; i++)
        {
            for (int j = 0; j < arrayCols; j++)
            {

                arrayTimes[i, j] = '*';
                Console.Write("{0}",arrayTimes[i, j]);
            }
            Console.WriteLine();
        }

        Console.ReadKey();
相关问题