【问题标题】:how can I draw a triangle of asterisks using the while statement?如何使用 while 语句绘制星号三角形?
【发布时间】:2014-04-05 17:08:59
【问题描述】:

这是(不工作的代码),它应该打印下面的形状,但它不是:

static void Main(string[] args)
{
    int i = 1;
    int k = 5;
    int h = 1;

    while (i <= 5)
    {
        Console.WriteLine("");
        while (k > i)
        {
            Console.Write(" ");
            k--;
        }
        while (h <= i)
        {
            Console.Write("**");
            h++;
        }
        i++;
    }
    Console.ReadLine();

}

但是当我尝试使用 while 语句做同样的事情时,形状会完全混乱。

有什么帮助吗?

【问题讨论】:

  • 显示您的 while 代码。
  • 我们需要查看您的无效代码来修复您的无效代码,而不是您的有效代码 =)
  • 如何在代码只写双星号的情况下将单个星号放在顶部?
  • 提示:for(int i = 0; i &lt; 5; i++){yourcode;} 等于:int i = 0; while(i &lt; 5){ yourcode; i++ }
  • 了解什么不起作用非常重要。更重要的是,实际上,没有人真正关心你是否把三角形弄对了。寻求帮助完全违背了这个目标。

标签: c# for-loop while-loop


【解决方案1】:

您必须在循环中声明kh

static void Main(string[] args)
{
    int i = 1;
    while (i <= 5)
    {
        int k = 5;
        int h = 1;

        Console.WriteLine("");
        while (k > i)
        {
            Console.Write(" ");
            k--;
        }
        while (h <= i)
        {
            Console.Write("**");
            h++;
        }
        i++;
    }
    Console.ReadLine();
}

使用您当前的解决方案,在第一次外循环迭代之后,内循环什么都不做。

【讨论】:

    【解决方案2】:
    int NumberOfLines = 5;
    int count = 1;
    while (NumberOfLines-- != 0)
    {
        int c = count;
        while (c-- != 0)
        {
            Console.Write("*");
        }
        Console.WriteLine();
        count = count + 2;
    }
    

    就是这样,最简单的实现。

    【讨论】:

    • 除了缺少所需的星号间距。
    【解决方案3】:

    问题是ikh在进入最外层循环之前被初始化。在外循环内kh 被内循环改变。在第二次执行外循环时,kh 与之前运行内循环后留下的值相同。随着i 在外循环中递增,k 循环将不会进入,h 循环只会运行一次。

    想想在第二次执行时,hk 在最外层循环内应该有什么值。

    【讨论】:

      【解决方案4】:

      我不会为你解决,只会给你提示: 使用 3 个循环语句

      1. for line change
      2. for spaces (reverse loop)
      3. for printing * (odd series in this case) i.e. 2n-1
      

      签入第三个while语句h 并且只打印一个* 代替**

      在这里查看: http://ideone.com/xOB2OI

      【讨论】:

      • 你应该在第一个while循环中声明h,k
      【解决方案5】:

      其实我是通过 'for' 循环完成的,z 是高度,x 是边长。

      等腰三角形(x>z):

       public void Print(int x, int z)
              {
                  var peakStart = x;
                  var peakEnd = x;
      
                  for (int i = 0; i < z; i++)
                  {
                      for (int j = 0; j < 2 * x + 1; j++)
                      {
                          if (peakStart < 1.5 * x && j >= peakStart && j <= peakEnd)
                              Console.Write("*");
                          else
                              Console.Write(" ");
                      }
                      peakStart--;
                      peakEnd++;
                      Console.WriteLine("");
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-13
        • 2021-11-07
        • 1970-01-01
        • 2022-01-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多