2015-10-26 39 views
0

是一个试图以下列格式打印乘法表的新手。 我无法整齐地输出数字高达10或以上的表格。我已经写过5以下的数字。但是没有把表格放在6以上的数字以下。有人可以帮我如何从6下表打印为例如10我已经创建乘法表全格式排列

This is the output I have created for numbers upto 5. I need the same format for numbers upto 10 or more.

class Program 
{ 
    static void Main(string[] args) 
    { 
     for (int i = 1; i <= 10; i++) 
     { 
      for (int j = 1; j <= 5; j++) 
      { 
       Console.Write("{0} * {1} = {2} \t", j, i, i * j); 
      } 
     } 
     Console.ReadKey(); 
    } 
} 
+0

Hh,?? j <= 10; – Paparazzi

回答

0

你需要做的是正确的变量对齐到固定的宽度什么。你可以做这样的事情:

static string RightNumber(int i) 
{ 
    return i.ToString().PadLeft(3); 
} 

然后你Console.Write线是:

Console.Write("{0} * {1} = {2} \t", RightNumber(j), RightNumber(i), RightNumber(i * j)); 

什么,将要做的是让所有的数字将占用三个空格在一条线上。所以不是“3”,而是“3”。如果您只需要2(或4等),您可以在PadLeft方法中更改变量,但是我认为您的列表中将会增加到10 * 10。

+0

@krillar嗨感谢您向我解释padleft。不幸的是,它并没有解决问题。问题是由于o/p屏幕尺寸的限制。我希望在当前输出之下打印6以上数字的颜色(即最多5个)。使用当前代码,如果我们增加j值,第二行将具有6 * 1 = 6 7 * 1 = 7 ....等等而不是1 * 2 = 2 2 * 2 = 4 3 * 2 = 6 – chinz