0

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

在此处输入图像描述

但是,我不知道如何正确显示我的代码。下面是我的代码。我知道我很接近,我只是不确定我做错了什么。

/*
 * 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();
        }

    }
}
4

2 回答 2

1

添加 :

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

rowfor 语句开始之后

固定代码:

    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();
    }

结果 :

展示

于 2013-02-14T23:16:06.997 回答
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();

    }

在此处输入图像描述

于 2013-02-14T23:18:51.753 回答