【问题标题】:How to print a diamond pattern outline with c#如何用c#打印菱形图案轮廓
【发布时间】:2022-11-20 20:41:49
【问题描述】:

如何用c#打印菱形图案轮廓

这是实心钻石的代码,我想移除中间部分并保留边缘。

由此,

对此,

public void DiamondOne()  
       {  
           int i, j, count = 1, number;  
           Console.Write("Enter number of rows:");  
           number = int.Parse(Console.ReadLine());  
           count = number - 1;  
           for (j = 1; j <= number; j++)  
           {  
               for (i = 1; i <= count; i++)  
                   Console.Write(" ");  
               count--;  
               for (i = 1; i <= 2 * j - 1; i++)  
                   Console.Write("*");  
               Console.WriteLine();  
           }  
           count = 1;  
           for (j = 1; j <= number - 1; j++)  
           {  
               for (i = 1; i <= count; i++)  
                   Console.Write(" ");  
               count++;  
               for (i = 1; i <= 2 * (number - j) - 1; i++)  
                   Console.Write("*");  
               Console.WriteLine();  
           }  
           Console.ReadLine();  
  }  

先谢谢了

【问题讨论】:

  • 不要写一整行*,只写第一个和最后一个(检查i的值)并用空格填充其余部分

标签: c#


【解决方案1】:

我会选择这样的东西:

    public void Diamond()
    {
        Console.WriteLine("Enter number of rows:");
        bool isNumber = int.TryParse(Console.ReadLine(), out int rowsNr);

        if (!isNumber)
        {
            Console.WriteLine("Not a number!");
            return;
        }

        // print the upper half
        for (int rowIndex = 0; rowIndex < rowsNr - 1; rowIndex++)
        {
            for (int colIndex = 0; colIndex <= 2 * rowsNr; colIndex++)
            {
                if (colIndex == Math.Abs(rowsNr - rowIndex) || colIndex == Math.Abs(rowsNr + rowIndex))
                {
                    Console.Write("*");
                }
                else
                {
                    Console.Write(" ");
                }
            }
            Console.WriteLine();
        }

        // print the lower half
        for (int rowIndex = 1; rowIndex <= rowsNr; rowIndex++)
        {
            for (int colIndex = 0; colIndex <= 2 * rowsNr; colIndex++)
            {
                if (colIndex == rowIndex || colIndex == 2 * rowsNr - rowIndex)
                {
                    Console.Write("*");
                }
                else
                {
                    Console.Write(" ");
                }
            }

            Console.WriteLine();
        }
    }

基本上你正在寻找 x 和 y 坐标。 4 条边中的每条边都有自己的方程式(例如,左上角的方程式是 x == y),您可以在迭代时检查这些方程式。我将代码分成两部分,一部分用于上半部分,一部分用于下半部分。它更具可读性,你不会把太多的 if 语句放在一起而失去代码的意义。

【讨论】:

    【解决方案2】:

    这就是我想出的:

    // Get the number of rows
    int rows;
    do
    {
        Console.WriteLine("Enter number of rows:");
    } while (!int.TryParse(Console.ReadLine(), out rows));
    
    // Print diamond
    DiamondOne(rows, Console.Out);
    
    // Wait for key press
    Console.WriteLine("Press any key to exit");
    Console.ReadKey(true);
    
    static void DiamondOne(int rows, TextWriter output)
    {
        for (int currentRow = 0; currentRow < rows; currentRow++)
        {
            OutputRow(rows, output, currentRow);
        }
    
        for (int currentRow = rows - 2; currentRow >= 0; currentRow--)
        {
            OutputRow(rows, output, currentRow);
        }
    }
    
    static void OutputRow(int rows, TextWriter output, int currentRow)
    {
        int indentation = rows - currentRow - 1;
        int diamondCentre = Math.Max((currentRow * 2) - 1, 0);
    
        output.Write(new string(' ', indentation));
        output.Write('*');
        output.Write(new string(' ', diamondCentre));
        if (currentRow != 0) output.Write('*');
        output.WriteLine();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多