乘法table结构
Multiplication table structure
关于这个乘法结构的问题table..
如何创建空格以便输出如下所示:
1 X 9 + 2 = 11
12 X 9 + 3 = 111
123 X 9 + 4 = 1111
1234 X 9 + 5 = 11111
12345 X 9 + 6 = 111111
123456 X 9 + 7 = 1111111
1234567 X 9 + 8 = 11111111
代码:
#region MTABLE
int number1 = 1;
for (int i = 2; i <= 8; i++)
{
int number2 = number1 * 9 + i;
Console.WriteLine("{0} x {1} + {2} = {3} ", number1, 9, i, number2);
number1 = number1 * 10 + i;
for (int j = 1; j <= i; j++)
Console.Write(" ");
}
Console.ReadKey();
#endregion
我的代码当前输出:
1 x 9 + 2 = 11
12 x 9 + 3 = 111
123 x 9 + 4 = 1111
1234 x 9 + 5 = 11111
12345 x 9 + 6 = 111111
123456 x 9 + 7 = 1111111
1234567 x 9 + 8 = 11111111
你快到了,你只是想倒数而不是倒数。您可以通过条件中的 8 减去 i 来实现。
for (int j = 1; j <= 8 - i; j++)
Console.Write(" ");
将内循环移动到外循环的开头,从8-i开始倒数:
for (int i = 2; i <= 8; i++)
{
for (int j = 8-i; j >0; j--)
Console.Write(" ");
int number2 = number1 * 9 + i;
Console.WriteLine("{0} x {1} + {2} = {3} ", number1, 9, i, number2);
number1 = number1 * 10 + i;
}
关于这个乘法结构的问题table..
如何创建空格以便输出如下所示:
1 X 9 + 2 = 11
12 X 9 + 3 = 111
123 X 9 + 4 = 1111
1234 X 9 + 5 = 11111
12345 X 9 + 6 = 111111
123456 X 9 + 7 = 1111111
1234567 X 9 + 8 = 11111111
代码:
#region MTABLE
int number1 = 1;
for (int i = 2; i <= 8; i++)
{
int number2 = number1 * 9 + i;
Console.WriteLine("{0} x {1} + {2} = {3} ", number1, 9, i, number2);
number1 = number1 * 10 + i;
for (int j = 1; j <= i; j++)
Console.Write(" ");
}
Console.ReadKey();
#endregion
我的代码当前输出:
1 x 9 + 2 = 11
12 x 9 + 3 = 111
123 x 9 + 4 = 1111
1234 x 9 + 5 = 11111
12345 x 9 + 6 = 111111
123456 x 9 + 7 = 1111111
1234567 x 9 + 8 = 11111111
你快到了,你只是想倒数而不是倒数。您可以通过条件中的 8 减去 i 来实现。
for (int j = 1; j <= 8 - i; j++)
Console.Write(" ");
将内循环移动到外循环的开头,从8-i开始倒数:
for (int i = 2; i <= 8; i++)
{
for (int j = 8-i; j >0; j--)
Console.Write(" ");
int number2 = number1 * 9 + i;
Console.WriteLine("{0} x {1} + {2} = {3} ", number1, 9, i, number2);
number1 = number1 * 10 + i;
}