我是 C#(和一般编程)的新手,而且我被困在学校实验室。 一直在尝试制作乘法表 1-10,但出现我不理解的错误。
我使用 System.Array 得到了这个;使用 System.Linq;那么我做错了什么?我可能什么都没做,呵呵
static void Calculate(int[] numbers)
{
int[] CalculateMultTable = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Calculate(CalculateMultTable);
{
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = numbers[i] * numbers[i];
}
}
}
最佳答案
正如其他答案所指出的,您正在进行无限递归调用,并且可能会收到 StackOverflowException
。另外,我不知道你在用你的阵列做什么。你应该分配给它吗?它只是为了帮助循环打印正确的数字吗?你可以这样简单地做到这一点:
for(int i = 1; i <= 10; i++)
{
for(int j = 1; j <= 10; j++)
{
Console.Write((i * j).ToString() + "\t");
}
Console.WriteLine();
}
这会给你这个作为输出:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
如果您想将其保存到二维数组中,您可以删除我的 Console.WriteLine
并将 Console.Write
替换为如下代码:
int[,] multTable = new int[10,10];
for(int i = 1; i <= 10; i++)
{
for(int j = 1; j <= 10; j++)
{
multTable[i - 1, j - 1] = i * j;
}
}
您可能希望将方法的返回类型从 void
更改为 int[,]
,这是 int
的二维数组>s.
虽然我认为您会把它扔掉,但作为一个提示可以帮助您在将来节省一些输入,初始化您的数组比输入所有 10 个元素要容易得多。当它遵循这样的规则模式时,您可以只使用循环:
int[] x = new int[10];
for(int i = 0; i < 10; i++)
{
x[i] = i + 1;
}
//this will give you the same array.
//Doesn't save any typing for just 10 elements, but saves a ton if you need,
//for example, 100 elements from 1-100
关于c# - C# 中的乘法表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40472522/