具有用户定义的行数和列数的 C# 交错数组



我正在尝试创建一个程序,该程序将创建一个数组,用户将在其中输入行数和列数,然后代码将使用 0 到 100 之间的随机数填充数组。Visual Studio 在代码中未显示任何错误,但是当我输入列的值时,应用程序崩溃,并且应用程序显示:"未处理的异常:System.RankException:此处仅支持单维数组。

我不确定我哪里出错了,

但如果有人能指出我正确的方向,我将不胜感激。谢谢。

using System;
using System.Collections;
namespace RandomNumberApp
{
class RandomNumberApp
{
    static void Main()
    {
        //variables
        int row = 0,
            column = 0,
            largestRandom = 0,
            maxX = 0,
            maxY = 0;
        //method calls
        row = GetRow(row);
        column = GetColumn(column);
        int[,] randomArray = new int[row, column];
        FillArray(row, column, randomArray);
        largestRandom = GetLargestNumber(ref randomArray, row, column, out maxX, out maxY);

        DisplayResults(randomArray, largestRandom, maxX, maxY);
        //determine whether user wants to run the program again
        Console.WriteLine("Do you want to create another array?");
        Console.WriteLine("Press 'Y if yes; 'N' if no");
        char userResponse;
        userResponse = Convert.ToChar(Console.ReadLine());
        if (userResponse == 'Y' || userResponse == 'y')
            Main();
    }
    //method to ask the user to enter the row size for the array
    static int GetRow(int row)
    {
        //variables
        string rawRow;
        Console.Write("Please enter the number of rows for your array: ");
        rawRow = Console.ReadLine();
        row = Convert.ToInt32(rawRow);
        return row;
    }
    //method to ask the user to enter the column size for the array    
    static int GetColumn(int column)
    {
        //variables
        string rawColumn;
        Console.WriteLine("nPlease enter the number of columns for your array: ");
        rawColumn = Console.ReadLine();
        column = Convert.ToInt32(rawColumn);
        return column;
    }
    //method to fill the array with random numbers
    static int[,] FillArray(int row, int column, int[,] randomArray)
    {
        //creates a random variable to fill the array
        Random randFill = new Random();
        //loop to fill the array with random numbers
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < column; j++)
            {
                randomArray[i, j] = randFill.Next(0, 100);
            }
        }
        return randomArray;
    }
    //method to find the largest number
    static int GetLargestNumber(ref int[,] randomArray, int row, int column, out int maxX, out int maxY)
    {
        int max = int.MinValue;
        maxX = -1;
        maxY = -1;
        for (int x = 0; x < column; x++)
        {
            for (int y = 0; y < row; y++)
            {
                if (randomArray[x, y] > max)
                {
                    max = randomArray[x, y];
                    maxX = x;
                    maxY = y;
                }
            }
        }
        return max;
    }
    //method to display the results
    static void DisplayResults(int[,] randomArray, int largestRandom, int maxX, int maxY)
    {
        //display the array elements in a list
        for (int i = 0; i < randomArray.GetLength(0); i++)
            for (int j = 0; j < randomArray.GetLength(1); j++)
                Console.WriteLine("{0,-3}", randomArray[i, j]);
        Console.WriteLine("nLargest Number In Array: " + largestRandom);
        Console.WriteLine(String.Format("nIndex Of Largest Number:nX: {0}nY: {1}n ", maxX, maxY));
    }
}
}
int[,] randomArray = new int[row, column];

这不是一个锯齿状数组,这是一个二维数组。交错数组可以通过以下方式定义

 int[][] 

。查看 MSDN 关于交错阵列的文章

也许我可以推荐

List<List<int>> 

List<int[]> 

供您使用。

关于您的 main() 函数,我注意到的第一件事是您在实际获取行和列值之前初始化数组。当然,您需要获取用户对数组大小的输入,然后初始化数组?

如果我理解您正在尝试正确执行的操作,那么使用GetLargestNumber方法可能更有意义:

/// <summary>
    /// Returns the largest number in the array.
    /// </summary>
    /// <param name="randomArray">The array to search.</param>
    /// <param name="rows">The amount of rows in the array.</param>
    /// <param name="columns">The amount of columns in the array.</param>
    /// <param name="maxX">The x-position (column) of the largest number in the array.</param>
    /// <param name="maxY">The y-position (row) of the largest number in the array.</param>
    /// <returns></returns>
    static int GetLargestNumber(ref int[,] randomArray, int rows, int columns, out int maxX, out int maxY)
    {
        int max;
        maxX = -1;
        maxY = -1;
        for (int x = 0; x < columns; x++)
        {
            for (int y = 0; y < rows; y++)
            {
                if (randomArray[x, y] > max)
                {
                    max = randomArray[x, y];
                    maxX = x;
                    maxY = y;
                }
            }
        }

        return max;
    }

这意味着您不再需要调用 Array.IndexOf(如其他答案中所述)您得到的错误来源。

通过解决提到的问题,我得到这个:

static void Main()
    {
        //variables
        int row = 0,
            column = 0,
            largestRandom = 0,
            maxX = 0,
            maxY = 0;
        //method calls
        row = GetRow(row);
        column = GetColumn(column);
        int[,] randomArray = new int[column, row];
        FillArray(row, column, randomArray);
        largestRandom = GetLargestNumber(ref randomArray, row, column, out maxX, out maxY);

        DisplayResults(randomArray, largestRandom, maxX, maxY);
        //determine whether user wants to run the program again
        Console.WriteLine("Do you want to create another array?");
        Console.WriteLine("Press 'Y if yes; 'N' if no");
        char userResponse;
        userResponse = Convert.ToChar(Console.ReadLine());
        if (userResponse == 'Y' || userResponse == 'y')
            Main();
    }
    //method to ask the user to enter the row size for the array
    static int GetRow(int row)
    {
        //variables
        string rawRow;
        Console.Write("Please enter the number of rows for your array: ");
        rawRow = Console.ReadLine();
        row = Convert.ToInt32(rawRow);
        return row;
    }
    //method to ask the user to enter the column size for the array    
    static int GetColumn(int column)
    {
        //variables
        string rawColumn;
        Console.WriteLine("nPlease enter the number of columns for your array: ");
        rawColumn = Console.ReadLine();
        column = Convert.ToInt32(rawColumn);
        return column;
    }
    //method to fill the array with random numbers
    static int[,] FillArray(int row, int column, int[,] randomArray)
    {
        //creates a random variable to fill the array
        Random randFill = new Random();
        //loop to fill the array with random numbers
        for (int i = 0; i < column; i++)
        {
            for (int j = 0; j < row; j++)
            {
                randomArray[i, j] = randFill.Next(0, 100);
            }
        }
        return randomArray;
    }
    /// <summary>
    /// Returns the largest number in the array.
    /// </summary>
    /// <param name="randomArray">The array to search.</param>
    /// <param name="rows">The amount of rows in the array.</param>
    /// <param name="columns">The amount of columns in the array.</param>
    /// <param name="maxX">The x-position (column) of the largest number in the array.</param>
    /// <param name="maxY">The y-position (row) of the largest number in the array.</param>
    /// <returns></returns>
    static int GetLargestNumber(ref int[,] randomArray, int rows, int columns, out int maxX, out int maxY)
    {
        int max = int.MinValue;
        maxX = -1;
        maxY = -1;
        for (int x = 0; x < columns; x++)
        {
            for (int y = 0; y < rows; y++)
            {
                if (randomArray[x, y] > max)
                {
                    max = randomArray[x, y];
                    maxX = x;
                    maxY = y;
                }
            }
        }

        return max;
    }
    //method to display the results
    static void DisplayResults(int[,] randomArray, int largestRandom, int maxX, int maxY)
    {
        //display the array elements in a list
        for (int i = 0; i < randomArray.GetLength(0); i++)
            for (int j = 0; j < randomArray.GetLength(1); j++)
                Console.WriteLine("{0,-3}", randomArray[i, j]);
        Console.WriteLine("nLargest Number In Array: " + largestRandom);
        Console.WriteLine(String.Format("nIndex Of Largest Number:nX: {0}nY: {1}n ", maxX, maxY));
    }

这对我来说效果很好!

正如其他答案所指出的,您可以对代码进行许多优化 - 交错数组的使用就是其中之一。

相关内容

  • 没有找到相关文章

最新更新