-4

我正在尝试打印

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

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

使用“While”和“do - while”。

但我不知道如何解决这个问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 0;
            int b = 0;

            while (a <= 0)
            {
                while (b <= a)
                {
                    Console.Write("*");
                    b++;
                }
                a++;
                Console.WriteLine();         
            }
            Console.ReadLine();
        }
    }
}

我一直在尝试像上面那样接近,但我认为它永远不会奏效!

PS 我将如何改进我的编程逻辑?我觉得我缺乏逻辑思维。

4

4 回答 4

1

我认为这是一个家庭作业问题,我通常不会回答它们,但我认为这可能会帮助您更好地了解如何编程,所以让我尝试解释一下......

仔细想想你想做什么 - 你想先打印 1 颗星,然后在 5 颗星处停止,然后打印相反的内容。

所以,首先,命名你的变量是有意义的:

int numStars = 1;
int maxNumStars = 5;

接下来,您可以按照以下方式循环一些内容:

while( numStars <= maxNumStars) { ... }

首先,它可以让你更好地理解你的问题,其次,它变得可读和可调试。

您的最终程序可能如下所示:

    static void Main(string[] args)
    {
        int numStars = 1;
        int maxNumStars = 5;

        // Print the ascending number of stars
        while(numStars <= maxNumStars)
        {
            // Write numStars number of stars to the console using the string constructor:
            Console.WriteLine(new string('*', numStars));
            numStars++;
        }

        // Print the descending number of stars
        while (numStars >= 1)
        {
            // Write numStars number of stars to the console using the string constructor:
            Console.WriteLine(new string('*', numStars));
            numStars--;
        }

    }

再说一次,我不喜欢为一个人工作,但我希望它有足够的意义来帮助你在未来解决类似的问题。


编辑:

为了完整起见,要在任何地方使用循环,您的代码/循环可能如下所示:

        // Declare a variable for writing the stars to the console:
        int numWritten;

        // Print the ascending number of stars
        while(numStars <= maxNumStars)
        {
            // Reset the number of stars written:
            numWritten = 0;

            // Write the stars with a loop:
            while (++numWritten <= numStars)
                Console.Write("*");

            // End the line and increment the numStars variable:
            Console.WriteLine();
            numStars++;
        }
于 2018-05-23T15:27:49.260 回答
0

可能朝这个方向工作:

        int a = 0;
        int b = 0;
        string s = string.Empty;

        while (a < 5)
        {
            while (b <= a)
            {
                s += "*";
                b++;
            }
            Console.Write(s);
            s = string.Empty;
            a++;
b = 0;
        }    

希望它能给你一些想法......

于 2018-05-23T15:20:04.550 回答
0

这是许多方法之一:

class Program
{
    static void Main(string[] args)
    {
        int a = 0;
        int b = 0;
        int c = 4;
        while (a <= c)
        {
            b = 0;
            while (b <= a)
            {
                Console.Write("*");
                b++;
            }
            a++;
            Console.WriteLine();
        }
        Console.WriteLine();
        if (a > c)
        {
            a--;
            while (a >= 0)
            {
                b = a;
                while (b >= 0)
                {
                    Console.Write("*");
                    b--;
                }
                a--;
                Console.WriteLine();
            }
        }
        Console.ReadLine();
    }
}

}

于 2018-05-23T15:26:43.350 回答
0

使用 while 循环可能会令人困惑,所以我想出了这个递归解决方案(用于科学)

public static void Main()
{
    test();

}

public static void test(string lastOutPut = "",int maxBound = 5,bool goingup = true,int cursor = 0,int maxOutputs = 10){

    if(cursor>maxOutputs)return;
    Console.Write("\n");
    if(goingup){
        Console.Write(lastOutPut+="*");
        cursor++;
        test(lastOutPut,maxBound,lastOutPut.Length <= maxBound,cursor,maxOutputs);

    }else{
        lastOutPut=lastOutPut.Substring(0,lastOutPut.Length-1);
        Console.Write(lastOutPut);
        cursor++;
        test(lastOutPut,maxBound,lastOutPut.Length <= 0,cursor,maxOutputs);
    }


}
于 2018-05-23T15:47:14.120 回答