gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:01 25 4
gpt4 key购买 nike

我正在尝试创建一个程序,该程序将创建一个数组,用户将在其中输入行数和列数,然后代码将使用 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];

那不是锯齿状数组,那是二维数组。Jagged Arrays 可以通过定义

 int[][] 

...看看 msdn's关于锯齿状数组的文章

也许我可以推荐

List<List<int>> 

List<int[]> 

供您使用。

关于具有用户定义的行数和列数的 C# 交错数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30005050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com