【问题标题】:c# triangle with number nestedc#三角形与数字嵌套
【发布时间】:2016-04-15 00:56:12
【问题描述】:

需要一些家庭作业帮助

开发一个 C# 控制台应用程序,分别显示以下两 (2) 个模式,一个在另一个之下。使用 for 循环(提示:嵌套)生成模式。所有星号都应由 Console.Write("*"); 形式的单个语句显示。它显示了指向示例中所示数字值的星号。 Console.WriteLine(); 形式的语句可用于移动到下一行。依次记下每个数字的顺序。请记住,这是用于生成这两种模式的两组独立的循环。您将需要推断数字是如何计算的(它们是计算的结果)以及该计算将放置在循环结构中的什么位置。您不能将显示的数字硬编码到循环中。

试图获得以下模式,但我的代码让我得到了正确的数字和间距,但没有得到正确数量的星星,最后有数字。我需要合并我的写语句吗?我应该有一个使用计数器整数的公式)任何帮助表示赞赏。

*2
**4
***6
****8
*****10
******12
*******14
********16
*********18
**********20

**********20 
*********18
********16
*******14
******12
*****10
****8
***6
**4
*2

using System;
{
    public class Program
    {
        const string STAR = "*";
        const string SPACE = " ";
        const int COUNTER = 10;

        static void Main(string[] args)
        {
            firsthalf();

            Console.ReadLine();
        }
        static public void firsthalf()
        {
            for (int r = 0; r < COUNTER; r++)
            {
                for (int c = 0; c <= r; c++)
                {
                    Console.Write(STAR);
                    Console.Write("{0}", (r + 1) * 2);
                }
                Console.WriteLine();
            }
        }
    }
}

【问题讨论】:

    标签: c# nested


    【解决方案1】:

    将数字Console.Write("{0}", (r + 1) * 2);的打印语句放在第二个循环之外

    for (int r = 0; r < COUNTER; r++)
    {
         for (int c = 0; c <= r; c++)
              Console.Write(STAR);    
    
         Console.WriteLine("{0}", (r + 1) * 2);
    }
    

    【讨论】:

      【解决方案2】:
      class Program
      {
          static void Main(string[] args)
          {
              f(1, 5, ascending: true);
              f(1, 10, ascending: false);
              Console.ReadLine();
          }
      
          private static void f(int start, int count, bool ascending)
          {
              var indices = Enumerable.Range(start, count);
              if (!ascending) indices = indices.Reverse();
              foreach (int index in indices)
              {
                  for (int i = 1; i <= index; i++)
                  {
                      Console.Write("*");
                  }
                  Console.WriteLine(2 * index);
              }
          }
      }
      

      【讨论】:

        【解决方案3】:
        using System;
        
        namespace SOFAcrobatics
        {
            public static class Launcher
            {
                public static void Main ()
                {
                    Int32 n = 0;
                    Boolean locker = false;
                    for (Int32 i = 2 ; i <= 38 ; i+= 2)
                    {
                        n = ((i > 20) ? (40 - i) : i);
                        if (n == 20 && !locker)
                        {
                            locker = true;
                            i -= 2;
                        }
                        for (Int32 j = 1; j <= (n / 2); j++)
                        {
                            Console.Write('*');
                        }
                        Console.WriteLine(n);
                    }
                    Console.ReadKey(true);
                }
            }
        }
        

        【讨论】:

          【解决方案4】:

          综合了几个答案,我非常感谢您的帮助。

          using System;
          
          
          public class Program
          {
          const string STAR = "*";
          const string SPACE = " ";
          const int COUNTER = 10;
          
          public static void Main(string[] args)
          {
            firsthalf();
            sechalf();
          
            Console.ReadLine();
           }
          
           static public void firsthalf()
           {
            for (int r = 0; r < COUNTER; r++)
            {
               for (int c = 0; c <= r; c++)
                  Console.Write(STAR);
          
               Console.WriteLine("{0}", (r + 1) * 2);
            }
            Console.WriteLine();
          }
          
          
          static public void sechalf()
          {
            for (int r = COUNTER; r > 0; r--)
            {
               for (int c = 0; c < r; c++)
                  Console.Write(STAR);
          
               Console.WriteLine("{0}", (r + 0) * 2);
               //Console.WriteLine();
            }
            Console.WriteLine();
          }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-09-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-07-21
            相关资源
            最近更新 更多