我仍然在这里学习一些 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-loop 一起工作。我认为它的方式是:
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 循环以创建带星号的金字塔的正确方法是什么?