【发布时间】:2015-05-02 17:01:26
【问题描述】:
我正在尝试创建一个程序,该程序将创建一个数组,用户将在其中输入行数和列数,然后代码将使用 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));
}
}
}
【问题讨论】:
-
错误信息很清楚——Array.IndexOf() 只支持一维数组。也许在这里查看一些搜索锯齿状数组的解决方案:stackoverflow.com/questions/5180566/…
-
首先,当
row和column都设置为0时,您正在实例化数组int[,] randomArray = new int[row, column];,因此它共有0 个元素。其次,指出错误发生在哪一行会很有用。无论如何,我建议尽可能使用int[][]而不是int[,]。 -
Array.IndexOf适用于一维数组 - 您有一个二维数组,即定义为精确行数和列数的数组,不是锯齿状数组,即一个“数组数组”,其中每个数组可以有不同的长度。锯齿状数组以[][]而非[,]的形式声明
标签: c# arrays multidimensional-array user-input