乘法表显示不正确

时间:2013-02-14 23:10:15

标签: c# multiplication

我必须编写一个代码来制作10x10乘法表,其显示必须如下所示:

enter image description here

但是,我无法弄清楚如何正确显示我的代码。这是我的代码如下。我知道我很亲密,我只是不确定我做错了什么。

/*
 * This program displays a multiplication table of the product of every integer from 1 through 10
 * multiplied by every integer from 1 through 10.
 * 
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DisplayMultiplicationTable
{
    class Program
    {
        static void Main(string[] args)
        {
            int value = 10;

            for (int x = 1; x <= value; ++x)

                Console.Write("{0, 4}", x);
            Console.WriteLine();
            Console.WriteLine("_________________________________________");

            for (int x = 1; x <= value; ++x)

                Console.WriteLine("{0, 4}", x);

            for (int row = 1; row <= value; ++row)
            {
                for (int column = 1; column <= value; ++column)
                {

                    Console.Write("{0, 4}", row * column);

                }
                Console.WriteLine();

            }
            Console.ReadLine();
        }

    }
}

2 个答案:

答案 0 :(得分:1)

补充:

Console.Write("{0, 4}", row);

启动row for statement

之后的

固定代码:

    static void Main(string[] args)
    {
        int value = 10;

        Console.Write("    ");
        for (int x = 1; x <= value; ++x)
            Console.Write("{0, 4}", x);

        Console.WriteLine();
        Console.WriteLine("_____________________________________________");

        for (int row = 1; row <= value; ++row)
        {
            Console.Write("{0, 4}", row);
            for (int column = 1; column <= value; ++column)
            {
                Console.Write("{0, 4}", row * column);
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }

结果:

display

答案 1 :(得分:1)

    int value = 10;

    // Indent column headers
    Console.Write("{0, 4}", null);

    // Write column headers
    for (int x = 1; x <= value; ++x)
        Console.Write("{0, 4}", x);

    // Write column header seperator
    Console.WriteLine();
    Console.WriteLine("_____________________________________________");

    // Write the table
    for (int row = 1; row <= value; ++row)
    {
        // Write the row header
        Console.Write("{0, 4}", row);

        for (int column = 1; column <= value; ++column)
        {
            // Write the row values
            Console.Write("{0, 4}", row * column);
        }
        // Finish the line
        Console.WriteLine();

    }

enter image description here