我可以选择开始填充二维数组的位置吗? [英] Can I choose a position to start filling a 2d array at?
问题描述
对于这个程序,我已经要求用户输入数组大小,然后按时间顺序填充数字,直到数组已满。我想将此数组放入网格中。我想知道是否可以在某一点开始输入数组,比如它是数字1,2,3,4,5,6,7,8,9,10。我能否开始输入在像[0,2]这样的某个点,所以基本上不使用第一个插槽来使网格像;
For this program i've asked the user to input the array size then its filled with numbers in chronological order until the array is full. I Want to then put this array into the grid. I was wondering if it's possible to start the input of the array at a certain point, say its was the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9 10. Would i be able to start the input at a certain point like [0,2], so basically not use the first slots to make the grid like;
[] [] [1] [2] [3]
[4] [5] [6] [7] [8]
[9] [10] [] [] []
i想知道我是否以及如何做到这一点非常感谢你!
i was wondering if and how i would be able to do this Thank you in advance!
class Program
{
static void Main(string[] args)
{
int Height = 4;
int Width = 5;
int[,] grid = new int[Height, Width];
Console.Write("Input Number: ");
int number = int.Parse(Console.ReadLine());
int[] InputNumber = new int[number];
var randomNumbers = Enumerable.Range(1, number).ToArray();
/*
[0,0] [0,1] [0,2] [0,3] [0,4]
[1,0] [1,1] [1,2] [1,3] [1,4]
[2,0] [2,1] [2,2] [2,3] [2,4]
[3,0] [3,1] [3,2] [3,3] [3,4]*/
}
}
我尝试了什么:
我是c#编程的新手,我已经尝试了几乎所有的东西,花了这么多时间来试图弄清楚自己以及研究,但我找不到任何东西。
What I have tried:
i'm new to c# programming and i've tried just about everything, spending so many hours in the process trying to figure it out myself as well as researching,however i can't find anything.
推荐答案
我做了这个例子,希望它有所帮助:
I made this example, hope it helps:
static void Main(string[] args)
{
int Height = 4;
int Width = 5;
Console.Write("Height: ");
Height = int.Parse(Console.ReadLine());
Console.Write("Width: ");
Width = int.Parse(Console.ReadLine());
int[,] grid = new int[Height, Width];
//TEst
PrintGrid(grid, Height, Width);
//Console.ReadLine();
//Console.Write("Input Number: ");
Console.Write("Input Number of Start SLOT between 1 and " + (Height * Width) + ": ");
int startSlot = int.Parse(Console.ReadLine());
Console.Write("Input Number. How many SLOT to fill between 1 and " + ((Height * Width) -startSlot+1) + ": ");
int numberSlot = int.Parse(Console.ReadLine());
///int[] InputNumber = new int[numberSlot];
//var randomNumbers = Enumerable.Range(1, number).ToArray();
//grid[0, 2] = randomNumbers[0];//Put direct in the position
//Randoms numbers to insert
int[] numbers = new int[numberSlot];
Random randNum = new Random();
for (int i = 0; i < numberSlot; i++)
{
numbers[i] = randNum.Next(1, (Height * Width));
}
//Insert from the start position
int pos = 0;
int numberIndex = 0;
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{
//ACTUAL POSITION
pos++;
if (pos == startSlot || pos > startSlot) {
if (numberIndex < numberSlot) {
grid[i, j] = numbers[numberIndex];
numberIndex++;
}
}
}
}
PrintGrid(grid, Height, Width);
Console.ReadLine();
/*
[0,0] [0,1] [0,2] [0,3] [0,4]
[1,0] [1,1] [1,2] [1,3] [1,4]
[2,0] [2,1] [2,2] [2,3] [2,4]
[3,0] [3,1] [3,2] [3,3] [3,4]*/
}
static void PrintGrid(int[,] grid, int h,int w) {
Console.Write("\nPrint Grid\n");
for (int i = 0; i< h; i++) {
for (int j = 0; j < w; j++) {
Console.Write( (grid[i, j]).ToString() + " ");
}
Console.Write("\n");
}
}
这篇关于我可以选择开始填充二维数组的位置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!