乘法表使用两个嵌套循环,但我收到一个错误

时间:2017-09-14 01:26:12

标签: c# visual-studio-2015 nested-loops

我正在尝试创建一个像...这样的表。

期望的:

enter image description here

但是我的代码就像..

实际:

enter image description here

我的代码:

static void Main(string[] args)
    {
        for (int i = 0; i <= 9; i++)
        {
            Console.Write(i + "\t");
            for (int j = 1; j <= 9; j++)
            {
                if (i > 0) Console.Write(i * j + "\t");
                else Console.Write(j + "\t");
            }
            Console.Write("\n");
        }
        Console.ReadKey();
    }

如何添加*-, |

6 个答案:

答案 0 :(得分:2)

这可以通过许多不同的方式完成,有些可能会更好,根据您的代码,它可以是这样的: https://dotnetfiddle.net/hZZcqJ

using System;
//https://stackoverflow.com/questions/46209099/multiplication-table-using-two-nested-loops-but-i-am-getting-an-error

public class Program
{
    public static void Main()
    {
        for (int i = 0; i <= 9; i++)
        {
            if (i == 0)
            {
                Console.Write("*\t|\t");
            }
            else
            {

                Console.Write(i + "\t|\t");
            }

            for (int j = 1; j <= 9; j++)
            {
                if (i > 0)
                    if(j*i>9)
                        Console.Write(i * j + "\t");
                    else
                        Console.Write(i * j + " \t");
                else
                    Console.Write(j + "\t ");
            }

            Console.Write("\n");
            if (i == 0)
            {
                Console.Write("\n");
                for (int k = 0; k <= 10; k++)
                {
                    if(k>2)
                        Console.Write(" -\t");
                    else
                        Console.Write("-\t");
                }

                Console.Write("\n");
            }

            Console.Write("\n");
        }

    }
}

答案 1 :(得分:1)

嗯..为什么要仔细阅读所有代码,什么时候才能做到这一点简单

for (int i = 1; i < 11; i++)
        {
            for (int j = 1; j < 11; j++)
            {
                int resultat = i * j;
                Console.Write(resultat.ToString().PadLeft(4));
            }
            Console.WriteLine();
        }

答案 2 :(得分:0)

        for (int i = 0; i <= 9; i++)
        {
            if (i == 0)
            {
                Console.Write("*\t | \t");
            }
            else
            {
                Console.Write(i + "\t | \t");
            }


            for (int j = 1; j <= 9; j++)
            {
                if (i > 0)
                {
                    Console.Write(i * j + "\t");
                }
                else
                {
                    Console.Write(j + "\t");
                }
            }
            Console.Write("\n");

            if (i == 0)
            {
                Console.WriteLine("-------------------------");
            }
        }

答案 3 :(得分:0)

试试这个:)

static void Main(string[] args)
    {
        for (int i = 0; i < 11; i++)
        {
            PrintRow(i);
        }
        Console.ReadLine();
    }

    private static void PrintRow(int i)
    {
        var args = new List<string>();
        if (i == 0)
        {
            for (int k = 0; k < 11; k++)
            {
                if (k == 0)
                {
                    args.Add("*");
                }
                else
                {
                    if (k == 1)
                    {
                        args.Add("|");
                    }
                    else
                    {
                        args.Add((k - 1).ToString());
                    }
                }
            }   

        }
        else
        {
            if (i == 1)
            {
                for (int k = 0; k < 11; k++)
                {
                    args.Add("-----");
                }
            }
            else
            {
                for (int k = 0; k < 11; k++)
                {
                    if (k == 0)
                    {
                        args.Add((i - 1).ToString());
                    }
                    else
                    {
                        if (k == 1)
                        {
                            args.Add("|");
                        }
                        else
                        {
                            args.Add(((i - 1) * (k -1)).ToString());
                        }
                    }
                }
            }
        }

        Console.WriteLine(string.Format("{0,5}{1,5}{2,5}{3,5}{4,5}{5,5}{6,5}{7,5}{8,5}{9,5}{10,5}", args.ToArray()));
    }

答案 4 :(得分:0)

请检查:

for (int i = 0; i <= 9; i++)
{
    if i == 0
    {
        Console.Write("* \t");
    }
    else if i != 1
    {
        Console.Write(i > 1 ? i - 1 : i + "\t");
    }

    for (int j = 1; j <= 9; j++)
    {
        if i == 1
        {
            Console.Write(i + "\t - \t");
        }
        else
        {
            if j == 1
            {
                Console.Write(i + "\t | \t");
            }        

            if i > 0
            {
                Console.Write((i > 1 ? i - 1 : i) * j + "\t");
            }
            else
            {
                Console.Write(j + "\t");
            }
        }
        j = j + 1
    }
    Console.Write("\n");
}

答案 5 :(得分:0)

namespace PracticingCSharp
{
    class Program

    {
        static void Main(string[] args)
        {
            for (int x = 1; x <= 10; x++)
            {
                Console.WriteLine();
                for (int y = 1; y <= 10; y++)
                {
                    Console.Write( x*y +"\t");
                }
            }
            Console.ReadLine();
        }
    }
}