使用while循环在C#中创建金字塔

时间:2013-08-15 17:49:50

标签: c# for-loop while-loop

我在这里学习了一些C#,但我用for循环制作了一个星号金字塔:

using System;     

namespace Nimi{      
class Ohjelma{      
static void Main(){ 
for(;;){ 
Console.Write("Anna korkeus: ");     
string eka = Console.ReadLine();     
int luku = int.Parse(eka); 
//First, I made that if the number is 0 or lower, it will ask the number again. 
//Hence the endless loop at start.
if(luku <= 0){ 
    continue; 
} else { 
    for (int i = 0; i < luku; i++ ) 
    { 
                for (int k=i+1; k < luku; k++) 
                   { 
                        Console.Write(" "); 
                } 
                for (int j = 2*i+1; j > 0; j--) 
                { 
                        Console.Write("*"); 
                    } 
            Console.WriteLine(""); 
           } 
        break; 
    } 
} 
} 
} 
}

我只是出于好奇而想要知道这对于我仍然无法创建的while循环是如何工作的。我认为的方式是:

using System;     

namespace Nimi{      
class Ohjelma{      
static void Main(){ 
while(true){ 
// The While-loop version of endless loop. Not sure how different it is.
Console.Write("Anna korkeus: ");     
string eka = Console.ReadLine();     
int luku = int.Parse(eka); 
if(luku <= 0){ 
    continue; 
} else {
    int i = 0;
    int j = i * 2 + 1;
    int k = i+1;
    while(i < luku)
    {
        while (j > 0){
            while (k < luku){
            Console.Write(" ");
            k++;
            }
        Console.Write("*");
        j--;
        }
    Console.WriteLine();
    i++;
    }
    break;
    }
} 
} 
} 
}

不要真的锻炼身体。它只发布这样的东西(当值为4时:

   *

从for-loop转换到while循环以创建带星号的金字塔的正确方法是什么?

3 个答案:

答案 0 :(得分:1)

using System;

namespace Nimi
{
    class Ohjelma
    {
        static void Main()
        {
            for (; ; )
            {
                Console.Write("Anna korkeus: ");
                string eka = Console.ReadLine();
                int luku = int.Parse(eka);
                //First, I made that if the number is 0 or lower, it will ask the number again. 
                //Hence the endless loop at start.
                if (luku <= 0)
                {
                    continue;
                }
                else
                {
                    int i = 0;

                    while (i < luku)
                    {
                        int k = i + 1;

                        while (k < luku)
                        {
                            Console.Write(" ");
                            k++;
                        }

                        int j = 2 * i + 1;

                        while (j > 0)
                        {
                            Console.Write("*");
                            j--;
                        }

                        Console.WriteLine("");

                        i++;
                    }

                    break;
                }
            }
        }
    }
}

请记住:如果您的代码格式正确,则更容易阅读。在Visual Studio中使用ED来格式化所有内容。

(^ ED表示CTRL + E然后是D(使用D你不需要CTRL,它将双向工作)

答案 1 :(得分:1)

            for (int j = 2*i+1; j > 0; j--) 
            { 
                    Console.Write("*"); 
                }

成为

{
    int j = 2*i+1;
    while (j > 0)
    {
        Console.Write("*");

        j--;
    }
}

请注意,“额外”花括号会保留j的位置,就像for()一样。

答案 2 :(得分:0)

     private static void pyramid()
    {
         int k = 10;
        int j=0;
        while(true)
        {
            int i = 0;
            while(true)
            {
                if (i >= (k - j) && i <= (k + j))
                {
                    Console.Write("*");
                    Console.Write("\t");
                }
                else
                {
                    Console.Write("\t");
                }
                if (i > (j + k))
                {
                    break;
                }
                i++;
            }
            Console.Write("\n");
            if (j == (k - 1))
            {
                break;
            }

                j++;
        }

    }