简体   繁体   English

2D数组,以奇怪的模式添加它的值

[英]2d array,adding it values in a weird pattern

i started learning C# and programming a few months ago and have some problems. 我几个月前开始学习C#和编程,遇到了一些问题。 The idea here is we create a 2 dimensional array (the number of rows / columns are added by the user), the numbers need to be between 1 and 10. 这里的想法是我们创建一个二维数组(用户添加的行数/列数),该数字必须在1到10之间。

Then when the array is created the number sequence ( 3-5-7-9-11 etc) is started in the first and finishes in the last column. 然后,当创建数组时,数字序列(3-5-7-9-11等)从第一列开始,到最后一列结束。 The rest of the numbers in the columns are added via keyboard by the user starting with the first row (ignoring column 1 and the last column cause we have that added). 用户通过键盘从第一行开始添加列中的其余数字(忽略列1和最后一列,因为我们添加了该列)。

The questions are : 问题是:

  • What will be the best way to check if the numbers of rows/columns are between 1 and 10? 检查行数/列数是否在1到10之间的最佳方法是什么? (I was thinking of IF-else but isn't there a better way ?) (我当时在想IF-else,但是没有更好的方法吗?)
  • How will i make it so that the number sequence 3-5-7 etc is started in the first and finishes in the last column? 我将如何使数字序列3-5-7等在第一列中开始并在最后一列中结束? Yeah i feel lost. 是的,我感到迷茫。

Where i am at the moment : 我现在在哪里:

        Console.WriteLine("Add row value of 1-10");
        string s1
        s1 = Console.ReadLine();
        int k = int.Parse(s1);

        Console.WriteLine("Add column value of 1-10");

        string s2;
        s2 = Console.ReadLine();
        int p = int.Parse(s2);
        int[,] M = new int[k, p];

Example : we added k(row) & p(coulmn) value of 4.So the array should look like : 示例:我们将k(row)和p(coulmn)的值相加为4,因此数组应类似于:

3 xx 11 3 xx 11

5 xx 13 5 xx 13

7 xx 15 7 xx 15

9 xx 17 9 xx 17

Then the X's should be added again manually without overwriting the existing numbers .The value of the numbers doesnt matter. 然后应再次手动添加X,而不会覆盖现有数字。数字的值无关紧要。

So... If I get it right you want to ask user the "length and width" of dynamical 2d array? 所以...如果我做对了,您想问用户动态二维数组的“长度和宽度”吗? To check if entered number is between 1 and 10 there's only 1 method: 要检查输入的数字是否在1到10之间,只有一种方法:

int [,] M;

if (k >= 1 && k <= 10 && p >= 1 && p <= 10)
{
    M = new int[k,p];
}

And better is to do int.TryParse() for case if user enters characters there instead of numbers, or else you can easily get an Exception . 更好的方法是int.TryParse() ,以防用户在其中输入字符而不是数字,否则很容易得到Exception

Filling with numbers: 用数字填充:

int num = 3;
for (int i = 0; i < k; ++i)
{
    M[i,0] = num;
    num+=2;
}

for (int i = 0; i < k; ++i)
{
    M[i,p] = num;
    num+=2;
}

This adds numbers in 1st and last column in each row. 这将在每行的第一列和最后一列中添加数字。 After that to fill other cells manually you check every cell thet it is not in firs or last column. 之后,要手动填充其他单元格,请检查每个单元格,它们不在fir或最后一列中。 I hope I understood you correctly. 我希望我能正确理解你。 Provided code may be simplified, but provided in such way for better understanding. 提供的代码可以简化,但是以更好地理解的方式提供。

if(k>0 && k<11 && p>0 && p<11)
{
 int i;
 int M[,] = new int[k,p];
 for (i=0;i<k;i++)
 {
  M[i,0]=i*2+3;
  M[i,p-1]=(i+k)*2+3;
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM