【问题标题】:How can i make patterns using do while loops in C#如何在 C# 中使用 do while 循环制作模式
【发布时间】:2017-12-03 21:30:43
【问题描述】:

谈到循环,我是个新手。 :( 请帮我。

问题:

使用do 循环,绘制以下图案:

*   
**
***
****
*****

【问题讨论】:

  • 这看起来像是一个学校作业,你自己尝试过吗?
  • 用 2 个嵌套的 do-loops 试试,不要等待答案。
  • 这不是一个教程网站。阅读How to Ask 并获取tour
  • 这很容易..我实际上使用 AddRange() 方法提示做了这件事.. 谷歌搜索如何使用它..

标签: c# do-while do-loops


【解决方案1】:
class MainClass
{
    public static void Main (string[] args)
    {
        int counter = 1;

        do {

            for (int i = 0; i < counter; i++) {
                Console.Write("*");
            }
            Console.WriteLine(); // for newline
            counter++;   // increase counter

        } while (counter < 6);
    }
}

抱歉我的英语不好。 您可以在 do-while 循环中使用计数器

【讨论】:

    【解决方案2】:

    你可以创建一个生成星星字符串的方法:

    public static string starGenerator(int count)
    {
        string stars = string.Empty;
        for(int i = 0; i < count; i++)
            stars += "*";
    
        return stars;
    }
    

    然后使用它:

    public static void Main(string[] args)
    {
        int counter = 1;
        do
        {
            string stars = starGenerator(counter);
            Console.WriteLine(stars);
    
            counter++;
        } while(counter <= 5);
    }
    

    【讨论】:

      【解决方案3】:

      有很多方法可以实现,如果你坚持do..while

      string line = "";
      
      do {
        Console.WriteLine(line += "*");
      }
      while (line.Length < 6);
      

      【讨论】:

        猜你喜欢
        • 2021-05-22
        • 2018-05-09
        • 1970-01-01
        • 2010-10-19
        • 2022-09-22
        • 1970-01-01
        • 2021-07-18
        • 1970-01-01
        相关资源
        最近更新 更多