【问题标题】:Using a while loop to create a pyramid in C#使用 while 循环在 C# 中创建金字塔
【发布时间】:2013-08-15 17:49:50
【问题描述】:

我仍然在这里学习一些 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循环转移到while循环以创建带星号的金字塔的正确方法是什么?

【问题讨论】:

标签: c# for-loop while-loop


【解决方案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;
                }
            }
        }
    }
}

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

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

【讨论】:

  • 这绝对有帮助 :) 实际上不,我没有为此使用 Visual Studio。这几乎是一个网站。哦,是的。你一开始还有 for(;;) 。将其更改为 while(true) 并没有问题。
【解决方案2】:
            for (int j = 2*i+1; j > 0; j--) 
            { 
                    Console.Write("*"); 
                }

变成

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

        j--;
    }
}

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

【讨论】:

    【解决方案3】:
         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++;
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-21
      • 2013-05-09
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 2014-10-25
      • 2022-09-29
      • 2023-03-26
      相关资源
      最近更新 更多