使用'while'和'do'制作简单的程序

时间:2018-05-23 15:11:59

标签: c#

我正在尝试打印

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

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

使用'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();
        }
    }
}

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

P.S我如何改进编程逻辑?我觉得我缺乏逻辑思维。

4 个答案:

答案 0 :(得分: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++;
        }

答案 1 :(得分: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;
        }    

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

答案 2 :(得分: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();
    }
}

}

答案 3 :(得分: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);
    }


}
相关问题