乘法表结构

时间:2018-02-08 20:25:10

标签: c# multiplication tabular

关于这个乘法表的结构的问题..

如何创建空格以使输出如下:

      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

2 个答案:

答案 0 :(得分:2)

将内循环移动到外循环的开头,并从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;
}

答案 1 :(得分:0)

你很亲密,你只想倒计时而不是倒计时。你可以通过在条件中从8中减去i来做到这一点。

for (int j = 1; j <= 8 - i; j++)
    Console.Write(" ");