【问题标题】:C# Multiplication TableC# 乘法表
【发布时间】:2012-07-03 01:32:35
【问题描述】:

所以我试图在 C# 中打印一个乘法表,但是我不太清楚如何得到我需要的东西。

到目前为止,我的程序输出以下内容:

1 2 3
2 4 6
3 6 9

但是,我需要它来输出这个:

0 1 2 3
1 1 2 3
2 2 4 6
3 3 6 9

我尝试了很多不同的方法来获得第二个输出,但我无法完全弄清楚。我不一定要求答案,但如果有人能指出我正确的方向,我将不胜感激。

这是我目前拥有的代码:

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

        Console.ReadLine();
    }

【问题讨论】:

  • 第二个输出的第一行应该很简单。只需打印从 0 到 n 的数字。对于其他行,只需在打印乘法表之前打印一个额外的数字。您应该知道要打印什么,对吧?
  • 你应该在这个问题中添加作业标签吗?
  • 你需要什么乘法表?

标签: c# loops multiplication


【解决方案1】:
for (int i = 0; i <= 3; i++)
{
    Console.Write(i + "\t");
    for (int j = 1; j <= 3; j++)
    {
        if (i>0) Console.Write(i * j + "\t");
        else Console.Write(j + "\t");
    }
    Console.Write("\n");
}

【讨论】:

    【解决方案2】:
    int tbl= int.Parse(Console.ReadLine());
    int j = int.Parse(Console.ReadLine());
    
    for (int i=1; i<=10; i++)
    {
         for (int j=1;j<=10; j++)
         {
              Console.WriteLine("{0}*{1}={2}", i, j, (i * j));`enter code here`
         }
    }
    Console.ReadLine();
    

    【讨论】:

      【解决方案3】:

      你应该跳过两个 0。

      for (int i = 0; i <= 3; i++)
      {
           for (int j = 0; j <= 3; j++)
           {
                Console.Write((i == 0? j : (j == 0? i : i*j)) + "\t");
           }
           Console.Write("\n");
      }
      

      【讨论】:

        【解决方案4】:

        您可以尝试这三种解决方案之一。

        解决方案 1(没有 if else 语句):

        static void Main(string[] args)
        {
            for (int i = 0; i <= 3; i++)
            {
                Console.Write("{0}\t", i);
                for (int j = 1; j <= 3; j++)
                {
                    Console.Write("{0}\t", i * j);
                }
                Console.WriteLine();
            }
        
            Console.ReadLine();
        }
        

        解决方案 2(使用 if else 语句):

        static void Main(string[] args)
        {
            for (int i = 0; i <= 3; i++)
            {
                for (int j = 1; j <= 3; j++)
                {
                    if (i == 0)
                    {
                        Console.Write("{0}\t", i);
                    }
                    else
                    {
                        Console.Write("{0}\t", i * j);
                    }
                }
                Console.WriteLine();
            }
        
            Console.ReadLine();
        }
        

        解决方案 3(使用简写 if else 语句):

        static void Main(string[] args)
        {
            for (int i = 0; i <= 3; i++)
            {
                for (int j = 1; j <= 3; j++)
                {
                    Console.Write("{0}\t", (i == 0) ? i : i * j);
                }
                Console.WriteLine();
            }
        
            Console.ReadLine();
        }
        

        【讨论】:

          【解决方案5】:
           for (int i = 0; i <= 3; i++)
                  {
                      for (int j = 0; j <= 3; j++)
                      {
          
                          if (i == 0)
                          {
                              Console.Write(j);
                          }
                          else
                          {
                              if(j == 0)
                              {
                                  Console.Write(i);
                              }
                              else
                              {
                                  Console.Write(i * j);
                              }
                          }
                      }
                      Console.Write("\n");
                  }
          

          【讨论】:

            【解决方案6】:
            Console.WriteLine("Enter A Number");
            int j = Convert.ToInt32(Console.ReadLine());
            for (int i = 0 ; i <= 10; i++) {
                Console.WriteLine("{1} X {0} = {2}",i,j,i*j);
                Console.ReadLine();
            }
            

            【讨论】:

              【解决方案7】:
              using System;
              /*
               * Write a console-based application that displays a multiplication table of the product of 
               * every integer from 1 through 10 multiplied by every integer from 1 through 10. Save the 
               * file as DisplayMultiplicationTable.cs.
               */
              
              namespace MultiplicationTable
              {
                  class Program
                  {
                      static void Main(string[] args)
                      {
                          Console.WriteLine("\t\t\t\t\t\t\t\t\tMultiplication Table");
                          Console.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------------------------");
                          const int END = 11;
                          for(int x = 1; x < END; x++)
                          {
                              for(int y = 1; y < END; y++)
                              {
                                  int value = x * y;
                                  Console.Write("{0} * {1} = {2}\t", y, x, value);
                              }
                              Console.WriteLine();
                          }
                          Console.ReadLine();
                      }
                  }
              }
              

              输出

              Output of Code

              我正在尝试在 GUI 中完成上述代码。到目前为止,我已经想出了以下代码;但输出不像上面的输出。

              我的 GUI 代码如下:

              使用系统; 使用 System.Windows.Forms;

              命名空间 DisplayMultiplicationTableGUI { 公共部分类Form1:Form { 公共表格1() { 初始化组件(); }

                  private void ShowTableButton_Click(object sender, EventArgs e)
                  {
                      int a;
                      int b;
                      const int STOP = 11;
              
                      for(a = 1; a < STOP; a++)
                      {
                          for(b = 1; b < STOP; b++)
                          {
                              int value = a * b; 
                              multiplicationTableLabel.Text += String.Format("{0} * {1} = {2}     ", b, a, value);
                          }
                          multiplicationTableLabel.Text += "\n";
                      }
                  }
              }
              

              }

              【讨论】:

              • 欢迎来到 Stack Overflow。您能否通过添加至少几个词来帮助解释来改进您的答案? (使用edit 按钮)
              • 请添加一些说明。你想做什么,有什么问题等等。
              • 我正在为帖子提供答案。
              猜你喜欢
              • 2020-02-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-07-19
              • 2015-06-09
              • 2019-12-13
              相关资源
              最近更新 更多